1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.codehaus.groovy.runtime;
20
21 import groovy.io.FileType;
22 import groovy.io.GroovyPrintWriter;
23 import groovy.lang.*;
24 import groovy.transform.stc.ClosureParams;
25 import groovy.transform.stc.FirstParam;
26 import groovy.transform.stc.FromString;
27 import groovy.transform.stc.MapEntryOrKeyValue;
28 import groovy.transform.stc.SimpleType;
29 import groovy.util.ClosureComparator;
30 import groovy.util.GroovyCollections;
31 import groovy.util.MapEntry;
32 import groovy.util.OrderBy;
33 import groovy.util.PermutationGenerator;
34 import groovy.util.ProxyGenerator;
35 import org.codehaus.groovy.classgen.Verifier;
36 import org.codehaus.groovy.reflection.ClassInfo;
37 import org.codehaus.groovy.reflection.MixinInMetaClass;
38 import org.codehaus.groovy.reflection.ReflectionCache;
39 import org.codehaus.groovy.reflection.stdclasses.CachedSAMClass;
40 import org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper;
41 import org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker;
42 import org.codehaus.groovy.runtime.dgmimpl.NumberNumberDiv;
43 import org.codehaus.groovy.runtime.dgmimpl.NumberNumberMinus;
44 import org.codehaus.groovy.runtime.dgmimpl.NumberNumberMultiply;
45 import org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus;
46 import org.codehaus.groovy.runtime.dgmimpl.arrays.*;
47 import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
48 import org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack;
49 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
50 import org.codehaus.groovy.runtime.typehandling.GroovyCastException;
51 import org.codehaus.groovy.runtime.typehandling.NumberMath;
52 import org.codehaus.groovy.tools.RootLoader;
53 import org.codehaus.groovy.transform.trait.Traits;
54 import org.codehaus.groovy.util.ArrayIterator;
55
56 import java.io.*;
57 import java.lang.reflect.Array;
58 import java.lang.reflect.Field;
59 import java.lang.reflect.Method;
60 import java.lang.reflect.Modifier;
61 import java.lang.reflect.Proxy;
62 import java.math.BigDecimal;
63 import java.math.BigInteger;
64 import java.net.MalformedURLException;
65 import java.net.ServerSocket;
66 import java.net.Socket;
67 import java.net.URI;
68 import java.net.URISyntaxException;
69 import java.net.URL;
70 import java.security.AccessController;
71 import java.security.PrivilegedAction;
72 import java.text.MessageFormat;
73 import java.util.*;
74 import java.util.concurrent.BlockingQueue;
75 import java.util.logging.Logger;
76 import java.util.regex.Matcher;
77 import java.util.regex.Pattern;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
124
125 private static final Logger LOG = Logger.getLogger(DefaultGroovyMethods.class.getName());
126 private static final Integer ONE = 1;
127 private static final BigInteger BI_INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
128 private static final BigInteger BI_INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
129 private static final BigInteger BI_LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
130 private static final BigInteger BI_LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
131
132 public static final Class [] additionals = {
133 NumberNumberPlus.class,
134 NumberNumberMultiply.class,
135 NumberNumberMinus.class,
136 NumberNumberDiv.class,
137 ObjectArrayGetAtMetaMethod.class,
138 ObjectArrayPutAtMetaMethod.class,
139 BooleanArrayGetAtMetaMethod.class,
140 BooleanArrayPutAtMetaMethod.class,
141 ByteArrayGetAtMetaMethod.class,
142 ByteArrayPutAtMetaMethod.class,
143 CharacterArrayGetAtMetaMethod.class,
144 CharacterArrayPutAtMetaMethod.class,
145 ShortArrayGetAtMetaMethod.class,
146 ShortArrayPutAtMetaMethod.class,
147 IntegerArrayGetAtMetaMethod.class,
148 IntegerArrayPutAtMetaMethod.class,
149 LongArrayGetAtMetaMethod.class,
150 LongArrayPutAtMetaMethod.class,
151 FloatArrayGetAtMetaMethod.class,
152 FloatArrayPutAtMetaMethod.class,
153 DoubleArrayGetAtMetaMethod.class,
154 DoubleArrayPutAtMetaMethod.class,
155 };
156
157 public static final Class[] DGM_LIKE_CLASSES = new Class[]{
158 DefaultGroovyMethods.class,
159 DateGroovyMethods.class,
160 EncodingGroovyMethods.class,
161 IOGroovyMethods.class,
162 ProcessGroovyMethods.class,
163 ResourceGroovyMethods.class,
164 SocketGroovyMethods.class,
165 StringGroovyMethods.class
166
167
168
169
170
171 };
172 private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
173
174
175
176
177
178
179
180
181
182
183
184
185 public static boolean is(Object self, Object other) {
186 return self == other;
187 }
188
189
190
191
192
193
194
195
196
197
198 public static <T> T identity(Object self, Closure<T> closure) {
199 return DefaultGroovyMethods.with(self, closure);
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public static <T,U> T with(
231 @DelegatesTo.Target("self") U self,
232 @DelegatesTo(value=DelegatesTo.Target.class,
233 target="self",
234 strategy=Closure.DELEGATE_FIRST)
235 @ClosureParams(FirstParam.class)
236 Closure<T> closure) {
237 @SuppressWarnings("unchecked")
238 final Closure<T> clonedClosure = (Closure<T>) closure.clone();
239 clonedClosure.setResolveStrategy(Closure.DELEGATE_FIRST);
240 clonedClosure.setDelegate(self);
241 return clonedClosure.call(self);
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255 public static Object getAt(Object self, String property) {
256 return InvokerHelper.getProperty(self, property);
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270 public static void putAt(Object self, String property, Object newValue) {
271 InvokerHelper.setProperty(self, property, newValue);
272 }
273
274
275
276
277
278
279
280
281
282 public static String dump(Object self) {
283 if (self == null) {
284 return "null";
285 }
286 StringBuilder buffer = new StringBuilder("<");
287 Class klass = self.getClass();
288 buffer.append(klass.getName());
289 buffer.append("@");
290 buffer.append(Integer.toHexString(self.hashCode()));
291 boolean groovyObject = self instanceof GroovyObject;
292
293
294
295
296
297 while (klass != null) {
298 for (final Field field : klass.getDeclaredFields()) {
299 if ((field.getModifiers() & Modifier.STATIC) == 0) {
300 if (groovyObject && field.getName().equals("metaClass")) {
301 continue;
302 }
303 AccessController.doPrivileged(new PrivilegedAction() {
304 public Object run() {
305 field.setAccessible(true);
306 return null;
307 }
308 });
309 buffer.append(" ");
310 buffer.append(field.getName());
311 buffer.append("=");
312 try {
313 buffer.append(InvokerHelper.toString(field.get(self)));
314 } catch (Exception e) {
315 buffer.append(e);
316 }
317 }
318 }
319
320 klass = klass.getSuperclass();
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 buffer.append(">");
350 return buffer.toString();
351 }
352
353
354
355
356
357
358
359
360
361
362
363 public static List<PropertyValue> getMetaPropertyValues(Object self) {
364 MetaClass metaClass = InvokerHelper.getMetaClass(self);
365 List<MetaProperty> mps = metaClass.getProperties();
366 List<PropertyValue> props = new ArrayList<PropertyValue>(mps.size());
367 for (MetaProperty mp : mps) {
368 props.add(new PropertyValue(self, mp));
369 }
370 return props;
371 }
372
373
374
375
376
377
378
379
380
381
382 public static Map getProperties(Object self) {
383 List<PropertyValue> metaProps = getMetaPropertyValues(self);
384 Map<String, Object> props = new LinkedHashMap<String, Object>(metaProps.size());
385
386 for (PropertyValue mp : metaProps) {
387 try {
388 props.put(mp.getName(), mp.getValue());
389 } catch (Exception e) {
390 LOG.throwing(self.getClass().getName(), "getProperty(" + mp.getName() + ")", e);
391 }
392 }
393 return props;
394 }
395
396
397
398
399
400
401
402
403
404
405 public static <T> T use(Object self, Class categoryClass, Closure<T> closure) {
406 return GroovyCategorySupport.use(categoryClass, closure);
407 }
408
409
410
411
412
413
414
415
416
417 public static void mixin(MetaClass self, List<Class> categoryClasses) {
418 MixinInMetaClass.mixinClassesToMetaClass(self, categoryClasses);
419 }
420
421
422
423
424
425
426
427
428
429 public static void mixin(Class self, List<Class> categoryClasses) {
430 mixin(getMetaClass(self), categoryClasses);
431 }
432
433
434
435
436
437
438
439
440 public static void mixin(Class self, Class categoryClass) {
441 mixin(getMetaClass(self), Collections.singletonList(categoryClass));
442 }
443
444
445
446
447
448
449
450
451 public static void mixin(Class self, Class[] categoryClass) {
452 mixin(getMetaClass(self), Arrays.asList(categoryClass));
453 }
454
455
456
457
458
459
460
461
462 public static void mixin(MetaClass self, Class categoryClass) {
463 mixin(self, Collections.singletonList(categoryClass));
464 }
465
466
467
468
469
470
471
472
473 public static void mixin(MetaClass self, Class[] categoryClass) {
474 mixin(self, Arrays.asList(categoryClass));
475 }
476
477
478
479
480
481
482
483
484
485
486 public static <T> T use(Object self, List<Class> categoryClassList, Closure<T> closure) {
487 return GroovyCategorySupport.use(categoryClassList, closure);
488 }
489
490
491
492
493
494
495
496
497 public static void addShutdownHook(Object self, Closure closure) {
498 Runtime.getRuntime().addShutdownHook(new Thread(closure));
499 }
500
501
502
503
504
505
506
507
508
509
510
511
512 public static Object use(Object self, Object[] array) {
513 if (array.length < 2)
514 throw new IllegalArgumentException(
515 "Expecting at least 2 arguments, a category class and a Closure");
516 Closure closure;
517 try {
518 closure = (Closure) array[array.length - 1];
519 } catch (ClassCastException e) {
520 throw new IllegalArgumentException("Expecting a Closure to be the last argument");
521 }
522 List<Class> list = new ArrayList<Class>(array.length - 1);
523 for (int i = 0; i < array.length - 1; ++i) {
524 Class categoryClass;
525 try {
526 categoryClass = (Class) array[i];
527 } catch (ClassCastException e) {
528 throw new IllegalArgumentException("Expecting a Category Class for argument " + i);
529 }
530 list.add(categoryClass);
531 }
532 return GroovyCategorySupport.use(list, closure);
533 }
534
535
536
537
538
539
540
541
542
543 public static void print(Object self, Object value) {
544
545 if (self instanceof Writer) {
546 try {
547 ((Writer) self).write(InvokerHelper.toString(value));
548 } catch (IOException e) {
549
550 }
551 } else {
552 System.out.print(InvokerHelper.toString(value));
553 }
554 }
555
556
557
558
559
560
561
562
563 public static void print(PrintWriter self, Object value) {
564 self.print(InvokerHelper.toString(value));
565 }
566
567
568
569
570
571
572
573
574 public static void print(PrintStream self, Object value) {
575 self.print(InvokerHelper.toString(value));
576 }
577
578
579
580
581
582
583
584
585
586 public static void print(Closure self, Object value) {
587 Object owner = getClosureOwner(self);
588 InvokerHelper.invokeMethod(owner, "print", new Object[]{value});
589 }
590
591
592
593
594
595
596
597 public static void println(Object self) {
598
599 if (self instanceof Writer) {
600 PrintWriter pw = new GroovyPrintWriter((Writer) self);
601 pw.println();
602 } else {
603 System.out.println();
604 }
605 }
606
607
608
609
610
611
612
613
614 public static void println(Closure self) {
615 Object owner = getClosureOwner(self);
616 InvokerHelper.invokeMethod(owner, "println", EMPTY_OBJECT_ARRAY);
617 }
618
619 private static Object getClosureOwner(Closure cls) {
620 Object owner = cls.getOwner();
621 while (owner instanceof GeneratedClosure) {
622 owner = ((Closure) owner).getOwner();
623 }
624 return owner;
625 }
626
627
628
629
630
631
632
633
634
635 public static void println(Object self, Object value) {
636
637 if (self instanceof Writer) {
638 final PrintWriter pw = new GroovyPrintWriter((Writer) self);
639 pw.println(value);
640 } else {
641 System.out.println(InvokerHelper.toString(value));
642 }
643 }
644
645
646
647
648
649
650
651
652 public static void println(PrintWriter self, Object value) {
653 self.println(InvokerHelper.toString(value));
654 }
655
656
657
658
659
660
661
662
663 public static void println(PrintStream self, Object value) {
664 self.println(InvokerHelper.toString(value));
665 }
666
667
668
669
670
671
672
673
674
675 public static void println(Closure self, Object value) {
676 Object owner = getClosureOwner(self);
677 InvokerHelper.invokeMethod(owner, "println", new Object[]{value});
678 }
679
680
681
682
683
684
685
686
687
688 public static void printf(Object self, String format, Object[] values) {
689 if (self instanceof PrintStream)
690 ((PrintStream)self).printf(format, values);
691 else
692 System.out.printf(format, values);
693 }
694
695
696
697
698
699
700
701
702
703
704 public static String sprintf(Object self, String format, Object[] values) {
705 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
706 PrintStream out = new PrintStream(outputStream);
707 out.printf(format, values);
708 return outputStream.toString();
709 }
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739 public static void printf(Object self, String format, Object arg) {
740 if (self instanceof PrintStream)
741 printf((PrintStream) self, format, arg);
742 else if (self instanceof Writer)
743 printf((Writer) self, format, arg);
744 else
745 printf(System.out, format, arg);
746 }
747
748 private static void printf(PrintStream self, String format, Object arg) {
749 self.print(sprintf(self, format, arg));
750 }
751
752 private static void printf(Writer self, String format, Object arg) {
753 try {
754 self.write(sprintf(self, format, arg));
755 } catch (IOException e) {
756 printf(System.out, format, arg);
757 }
758 }
759
760
761
762
763
764
765
766
767
768
769
770
771
772 public static String sprintf(Object self, String format, Object arg) {
773 if (arg instanceof Object[]) {
774 return sprintf(self, format, (Object[]) arg);
775 }
776 if (arg instanceof List) {
777 return sprintf(self, format, ((List) arg).toArray());
778 }
779 if (!arg.getClass().isArray()) {
780 Object[] o = (Object[]) java.lang.reflect.Array.newInstance(arg.getClass(), 1);
781 o[0] = arg;
782 return sprintf(self, format, o);
783 }
784
785 Object[] ans;
786 String elemType = arg.getClass().getName();
787 if (elemType.equals("[I")) {
788 int[] ia = (int[]) arg;
789 ans = new Integer[ia.length];
790 for (int i = 0; i < ia.length; i++) {
791 ans[i] = ia[i];
792 }
793 } else if (elemType.equals("[C")) {
794 char[] ca = (char[]) arg;
795 ans = new Character[ca.length];
796 for (int i = 0; i < ca.length; i++) {
797 ans[i] = ca[i];
798 }
799 } else if (elemType.equals("[Z")) {
800 boolean[] ba = (boolean[]) arg;
801 ans = new Boolean[ba.length];
802 for (int i = 0; i < ba.length; i++) {
803 ans[i] = ba[i];
804 }
805 } else if (elemType.equals("[B")) {
806 byte[] ba = (byte[]) arg;
807 ans = new Byte[ba.length];
808 for (int i = 0; i < ba.length; i++) {
809 ans[i] = ba[i];
810 }
811 } else if (elemType.equals("[S")) {
812 short[] sa = (short[]) arg;
813 ans = new Short[sa.length];
814 for (int i = 0; i < sa.length; i++) {
815 ans[i] = sa[i];
816 }
817 } else if (elemType.equals("[F")) {
818 float[] fa = (float[]) arg;
819 ans = new Float[fa.length];
820 for (int i = 0; i < fa.length; i++) {
821 ans[i] = fa[i];
822 }
823 } else if (elemType.equals("[J")) {
824 long[] la = (long[]) arg;
825 ans = new Long[la.length];
826 for (int i = 0; i < la.length; i++) {
827 ans[i] = la[i];
828 }
829 } else if (elemType.equals("[D")) {
830 double[] da = (double[]) arg;
831 ans = new Double[da.length];
832 for (int i = 0; i < da.length; i++) {
833 ans[i] = da[i];
834 }
835 } else {
836 throw new RuntimeException("sprintf(String," + arg + ")");
837 }
838 return sprintf(self, format, ans);
839 }
840
841
842
843
844
845
846
847
848
849
850
851 public static String inspect(Object self) {
852 return InvokerHelper.inspect(self);
853 }
854
855
856
857
858
859
860
861
862 public static void print(Object self, PrintWriter out) {
863 if (out == null) {
864 out = new PrintWriter(System.out);
865 }
866 out.print(InvokerHelper.toString(self));
867 }
868
869
870
871
872
873
874
875
876 public static void println(Object self, PrintWriter out) {
877 if (out == null) {
878 out = new PrintWriter(System.out);
879 }
880 out.println(InvokerHelper.toString(self));
881 }
882
883
884
885
886
887
888
889
890
891
892
893 public static Object invokeMethod(Object object, String method, Object arguments) {
894 return InvokerHelper.invokeMethod(object, method, arguments);
895 }
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915 public static boolean isCase(Object caseValue, Object switchValue) {
916 if (caseValue.getClass().isArray()) {
917 return isCase(DefaultTypeTransformation.asCollection(caseValue), switchValue);
918 }
919 return caseValue.equals(switchValue);
920 }
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939 public static boolean isCase(Class caseValue, Object switchValue) {
940 if (switchValue instanceof Class) {
941 Class val = (Class) switchValue;
942 return caseValue.isAssignableFrom(val);
943 }
944 return caseValue.isInstance(switchValue);
945 }
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965 public static boolean isCase(Collection caseValue, Object switchValue) {
966 return caseValue.contains(switchValue);
967 }
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986 public static boolean isCase(Map caseValue, Object switchValue) {
987 return DefaultTypeTransformation.castToBoolean(caseValue.get(switchValue));
988 }
989
990
991
992
993
994
995
996
997
998
999
1000 public static boolean isCase(Number caseValue, Number switchValue) {
1001 return NumberMath.compareTo(caseValue, switchValue) == 0;
1002 }
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 public static <T> Iterator<T> unique(Iterator<T> self) {
1015 return toList((Iterable<T>) unique(toList(self))).listIterator();
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 public static <T> Collection<T> unique(Collection<T> self) {
1029 return unique(self, true);
1030 }
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042 public static <T> List<T> unique(List<T> self) {
1043 return (List<T>) unique((Collection<T>) self, true);
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065 public static <T> Collection<T> unique(Collection<T> self, boolean mutate) {
1066 List<T> answer = new ArrayList<T>();
1067 for (T t : self) {
1068 boolean duplicated = false;
1069 for (T t2 : answer) {
1070 if (coercedEquals(t, t2)) {
1071 duplicated = true;
1072 break;
1073 }
1074 }
1075 if (!duplicated)
1076 answer.add(t);
1077 }
1078 if (mutate) {
1079 self.clear();
1080 self.addAll(answer);
1081 }
1082 return mutate ? self : answer ;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 public static <T> List<T> unique(List<T> self, boolean mutate) {
1105 return (List<T>) unique((Collection<T>) self, mutate);
1106 }
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117 public static int numberAwareCompareTo(Comparable self, Comparable other) {
1118 NumberAwareComparator<Comparable> numberAwareComparator = new NumberAwareComparator<Comparable>();
1119 return numberAwareComparator.compare(self, other);
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 public static <T> Iterator<T> unique(Iterator<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
1140 return toList((Iterable<T>) unique(toList(self), closure)).listIterator();
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 public static <T> Collection<T> unique(Collection<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
1164 return unique(self, true, closure);
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187 public static <T> List<T> unique(List<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
1188 return (List<T>) unique((Collection<T>) self, true, closure);
1189 }
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 public static <T> Collection<T> unique(Collection<T> self, boolean mutate, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
1219
1220 int params = closure.getMaximumNumberOfParameters();
1221 if (params == 1) {
1222 self = unique(self, mutate, new OrderBy<T>(closure, true));
1223 } else {
1224 self = unique(self, mutate, new ClosureComparator<T>(closure));
1225 }
1226 return self;
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 public static <T> List<T> unique(List<T> self, boolean mutate, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
1257 return (List<T>) unique((Collection<T>) self, mutate, closure);
1258 }
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 public static <T> Iterator<T> unique(Iterator<T> self, Comparator<T> comparator) {
1270 return toList((Iterable<T>) unique(toList(self), comparator)).listIterator();
1271 }
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320 public static <T> Collection<T> unique(Collection<T> self, Comparator<T> comparator) {
1321 return unique(self, true, comparator) ;
1322 }
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371 public static <T> List<T> unique(List<T> self, Comparator<T> comparator) {
1372 return (List<T>) unique((Collection<T>) self, true, comparator);
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422 public static <T> Collection<T> unique(Collection<T> self, boolean mutate, Comparator<T> comparator) {
1423 List<T> answer = new ArrayList<T>();
1424 for (T t : self) {
1425 boolean duplicated = false;
1426 for (T t2 : answer) {
1427 if (comparator.compare(t, t2) == 0) {
1428 duplicated = true;
1429 break;
1430 }
1431 }
1432 if (!duplicated)
1433 answer.add(t);
1434 }
1435 if (mutate) {
1436 self.clear();
1437 self.addAll(answer);
1438 }
1439 return mutate ? self : answer;
1440 }
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489 public static <T> List<T> unique(List<T> self, boolean mutate, Comparator<T> comparator) {
1490 return (List<T>) unique((Collection<T>) self, mutate, comparator);
1491 }
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517 public static <T> Iterator<T> toUnique(Iterator<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure condition) {
1518 return new UniqueIterator<T>(self, condition.getMaximumNumberOfParameters() == 1
1519 ? new OrderBy<T>(condition, true)
1520 : new ClosureComparator<T>(condition));
1521 }
1522
1523 private static final class UniqueIterator<E> implements Iterator<E> {
1524 private final Iterator<E> delegate;
1525 private final Set<E> seen;
1526 private boolean exhausted;
1527 private E next;
1528
1529 private UniqueIterator(Iterator<E> delegate, Comparator<E> comparator) {
1530 this.delegate = delegate;
1531 seen = new TreeSet<E>(comparator);
1532 advance();
1533 }
1534
1535 public boolean hasNext() {
1536 return !exhausted;
1537 }
1538
1539 public E next() {
1540 if (exhausted) throw new NoSuchElementException();
1541 E result = next;
1542 advance();
1543 return result;
1544 }
1545
1546 public void remove() {
1547 if (exhausted) throw new NoSuchElementException();
1548 delegate.remove();
1549 }
1550
1551 private void advance() {
1552 boolean foundNext = false;
1553 while (!foundNext && !exhausted) {
1554 exhausted = !delegate.hasNext();
1555 if (!exhausted) {
1556 next = delegate.next();
1557 foundNext = seen.add(next);
1558 }
1559 }
1560 }
1561 }
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573 public static <T> Iterator<T> toUnique(Iterator<T> self, Comparator<T> comparator) {
1574 return new UniqueIterator<T>(self, comparator);
1575 }
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585 public static <T> Iterator<T> toUnique(Iterator<T> self) {
1586 return new UniqueIterator<T>(self, null);
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634 public static <T> Collection<T> toUnique(Iterable<T> self, Comparator<T> comparator) {
1635 Collection<T> result = createSimilarCollection((Collection<T>) self);
1636 addAll(result, toUnique(self.iterator(), comparator));
1637 return result;
1638 }
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685 public static <T> List<T> toUnique(List<T> self, Comparator<T> comparator) {
1686 return (List<T>) toUnique((Iterable<T>) self, comparator);
1687 }
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703 public static <T> Collection<T> toUnique(Iterable<T> self) {
1704 return toUnique(self, (Comparator<T>) null);
1705 }
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721 public static <T> List<T> toUnique(List<T> self) {
1722 return toUnique(self, (Comparator<T>) null);
1723 }
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762 public static <T> Collection<T> toUnique(Iterable<T> self, @ClosureParams(value = FromString.class, options = {"T", "T,T"}) Closure condition) {
1763 Comparator<T> comparator = condition.getMaximumNumberOfParameters() == 1
1764 ? new OrderBy<T>(condition, true)
1765 : new ClosureComparator<T>(condition);
1766 return toUnique(self, comparator);
1767 }
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806 public static <T> List<T> toUnique(List<T> self, @ClosureParams(value = FromString.class, options = {"T", "T,T"}) Closure condition) {
1807 return (List<T>) toUnique((Iterable<T>) self, condition);
1808 }
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828 @SuppressWarnings("unchecked")
1829 public static <T> T[] toUnique(T[] self, Comparator<T> comparator) {
1830 Collection<T> items = toUnique(toList(self), comparator);
1831 T[] result = createSimilarArray(self, items.size());
1832 return items.toArray(result);
1833 }
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850 @SuppressWarnings("unchecked")
1851 public static <T> T[] toUnique(T[] self) {
1852 return (T[]) toUnique(self, (Comparator) null);
1853 }
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870 @SuppressWarnings("unchecked")
1871 public static <T> T[] toUnique(T[] self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure condition) {
1872 Comparator<T> comparator = condition.getMaximumNumberOfParameters() == 1
1873 ? new OrderBy<T>(condition, true)
1874 : new ClosureComparator<T>(condition);
1875 return toUnique(self, comparator);
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889 public static <T> T each(T self, Closure closure) {
1890 each(InvokerHelper.asIterator(self), closure);
1891 return self;
1892 }
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904 public static <T> T eachWithIndex(T self, Closure closure) {
1905 final Object[] args = new Object[2];
1906 int counter = 0;
1907 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
1908 args[0] = iter.next();
1909 args[1] = counter++;
1910 closure.call(args);
1911 }
1912 return self;
1913 }
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925 public static <T> Iterable<T> eachWithIndex(Iterable<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
1926 eachWithIndex(self.iterator(), closure);
1927 return self;
1928 }
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940 public static <T> Iterator<T> eachWithIndex(Iterator<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
1941 final Object[] args = new Object[2];
1942 int counter = 0;
1943 while (self.hasNext()) {
1944 args[0] = self.next();
1945 args[1] = counter++;
1946 closure.call(args);
1947 }
1948 return self;
1949 }
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961 public static <T> Collection<T> eachWithIndex(Collection<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
1962 return (Collection<T>) eachWithIndex((Iterable<T>) self, closure);
1963 }
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975 public static <T> List<T> eachWithIndex(List<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
1976 return (List<T>) eachWithIndex((Iterable<T>) self, closure);
1977 }
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989 public static <T> Set<T> eachWithIndex(Set<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
1990 return (Set<T>) eachWithIndex((Iterable<T>) self, closure);
1991 }
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003 public static <T> SortedSet<T> eachWithIndex(SortedSet<T> self, @ClosureParams(value=FromString.class, options="T,Integer") Closure closure) {
2004 return (SortedSet<T>) eachWithIndex((Iterable<T>) self, closure);
2005 }
2006
2007
2008
2009
2010
2011
2012
2013
2014 public static <T> Iterable<T> each(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2015 each(self.iterator(), closure);
2016 return self;
2017 }
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027 public static <T> Iterator<T> each(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2028 while (self.hasNext()) {
2029 Object arg = self.next();
2030 closure.call(arg);
2031 }
2032 return self;
2033 }
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043 public static <T> Collection<T> each(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2044 return (Collection<T>) each((Iterable<T>) self, closure);
2045 }
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055 public static <T> List<T> each(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2056 return (List<T>) each((Iterable<T>) self, closure);
2057 }
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067 public static <T> Set<T> each(Set<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2068 return (Set<T>) each((Iterable<T>) self, closure);
2069 }
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079 public static <T> SortedSet<T> each(SortedSet<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2080 return (SortedSet<T>) each((Iterable<T>) self, closure);
2081 }
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105 public static <K, V> Map<K, V> each(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure closure) {
2106 for (Map.Entry entry : self.entrySet()) {
2107 callClosureForMapEntry(closure, entry);
2108 }
2109 return self;
2110 }
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126 public static <K, V> Map<K, V> reverseEach(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure closure) {
2127 final Iterator<Map.Entry<K, V>> entries = reverse(self.entrySet().iterator());
2128 while (entries.hasNext()) {
2129 callClosureForMapEntry(closure, entries.next());
2130 }
2131 return self;
2132 }
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152 public static <K, V> Map<K, V> eachWithIndex(Map<K, V> self, @ClosureParams(value=MapEntryOrKeyValue.class, options="index=true") Closure closure) {
2153 int counter = 0;
2154 for (Map.Entry entry : self.entrySet()) {
2155 callClosureForMapEntryAndCounter(closure, entry, counter++);
2156 }
2157 return self;
2158 }
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171 public static <T> List<T> reverseEach(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2172 each(new ReverseListIterator<T>(self), closure);
2173 return self;
2174 }
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184 public static <T> T[] reverseEach(T[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
2185 each(new ReverseListIterator<T>(Arrays.asList(self)), closure);
2186 return self;
2187 }
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202 public static boolean every(Object self, Closure closure) {
2203 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2204 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
2205 if (!bcw.call(iter.next())) {
2206 return false;
2207 }
2208 }
2209 return true;
2210 }
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225 public static <T> boolean every(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2226 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2227 while (self.hasNext()) {
2228 if (!bcw.call(self.next())) {
2229 return false;
2230 }
2231 }
2232 return true;
2233 }
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248 public static <T> boolean every(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2249 return every(self.iterator(), closure);
2250 }
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267 public static <K, V> boolean every(Map<K, V> self, @ClosureParams(value=MapEntryOrKeyValue.class) Closure closure) {
2268 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2269 for (Map.Entry<K, V> entry : self.entrySet()) {
2270 if (!bcw.callForMap(entry)) {
2271 return false;
2272 }
2273 }
2274 return true;
2275 }
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287 public static boolean every(Object self) {
2288 BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker();
2289 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
2290 if (!bmi.convertToBoolean(iter.next())) {
2291 return false;
2292 }
2293 }
2294 return true;
2295 }
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306 public static boolean any(Object self, Closure closure) {
2307 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2308 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
2309 if (bcw.call(iter.next())) return true;
2310 }
2311 return false;
2312 }
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323 public static <T> boolean any(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2324 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2325 for (Iterator iter = self; iter.hasNext();) {
2326 if (bcw.call(iter.next())) return true;
2327 }
2328 return false;
2329 }
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340 public static <T> boolean any(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2341 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2342 for (Iterator<T> iter = self.iterator(); iter.hasNext();) {
2343 if (bcw.call(iter.next())) return true;
2344 }
2345 return false;
2346 }
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364 public static <K, V> boolean any(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> closure) {
2365 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2366 for (Map.Entry<K, V> entry : self.entrySet()) {
2367 if (bcw.callForMap(entry)) {
2368 return true;
2369 }
2370 }
2371 return false;
2372 }
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383 public static boolean any(Object self) {
2384 BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker();
2385 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
2386 if (bmi.convertToBoolean(iter.next())) {
2387 return true;
2388 }
2389 }
2390 return false;
2391 }
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412 public static Collection grep(Object self, Object filter) {
2413 Collection answer = createSimilarOrDefaultCollection(self);
2414 BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
2415 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
2416 Object object = iter.next();
2417 if (bmi.invoke(filter, object)) {
2418 answer.add(object);
2419 }
2420 }
2421 return answer;
2422 }
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443 public static <T> Collection<T> grep(Collection<T> self, Object filter) {
2444 Collection<T> answer = createSimilarCollection(self);
2445 BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
2446 for (T element : self) {
2447 if (bmi.invoke(filter, element)) {
2448 answer.add(element);
2449 }
2450 }
2451 return answer;
2452 }
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473 public static <T> List<T> grep(List<T> self, Object filter) {
2474 return (List<T>) grep((Collection<T>) self, filter);
2475 }
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496 public static <T> Set<T> grep(Set<T> self, Object filter) {
2497 return (Set<T>) grep((Collection<T>) self, filter);
2498 }
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519 public static <T> Collection<T> grep(T[] self, Object filter) {
2520 Collection<T> answer = new ArrayList<T>();
2521 BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
2522 for (T element : self) {
2523 if (bmi.invoke(filter, element)) {
2524 answer.add(element);
2525 }
2526 }
2527 return answer;
2528 }
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545 public static Collection grep(Object self) {
2546 return grep(self, Closure.IDENTITY);
2547 }
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564 public static <T> Collection<T> grep(Collection<T> self) {
2565 return grep(self, Closure.IDENTITY);
2566 }
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583 public static <T> List<T> grep(List<T> self) {
2584 return grep(self, Closure.IDENTITY);
2585 }
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602 public static <T> Set<T> grep(Set<T> self) {
2603 return grep(self, Closure.IDENTITY);
2604 }
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621 @SuppressWarnings("unchecked")
2622 public static <T> Collection<T> grep(T[] self) {
2623 return grep(self, Closure.IDENTITY);
2624 }
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638 public static Number count(Iterator self, Object value) {
2639 long answer = 0;
2640 while (self.hasNext()) {
2641 if (DefaultTypeTransformation.compareEqual(self.next(), value)) {
2642 ++answer;
2643 }
2644 }
2645
2646 if (answer <= Integer.MAX_VALUE) return (int) answer;
2647 return answer;
2648 }
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663 public static <T> Number count(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2664 long answer = 0;
2665 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2666 while (self.hasNext()) {
2667 if (bcw.call(self.next())) {
2668 ++answer;
2669 }
2670 }
2671
2672 if (answer <= Integer.MAX_VALUE) return (int) answer;
2673 return answer;
2674 }
2675
2676
2677
2678
2679
2680 @Deprecated
2681 public static Number count(Collection self, Object value) {
2682 return count(self.iterator(), value);
2683 }
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698 public static Number count(Iterable self, Object value) {
2699 return count(self.iterator(), value);
2700 }
2701
2702
2703
2704
2705
2706 @Deprecated
2707 public static Number count(Collection self, Closure closure) {
2708 return count(self.iterator(), closure);
2709 }
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722 public static <T> Number count(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
2723 return count(self.iterator(), closure);
2724 }
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739 public static <K,V> Number count(Map<K,V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> closure) {
2740 long answer = 0;
2741 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
2742 for (Object entry : self.entrySet()) {
2743 if (bcw.callForMap((Map.Entry)entry)) {
2744 ++answer;
2745 }
2746 }
2747
2748 if (answer <= Integer.MAX_VALUE) return (int) answer;
2749 return answer;
2750 }
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762 public static Number count(Object[] self, Object value) {
2763 return count((Iterable)Arrays.asList(self), value);
2764 }
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774 public static <T> Number count(T[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
2775 return count((Iterable)Arrays.asList(self), closure);
2776 }
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788 public static Number count(int[] self, Object value) {
2789 return count(InvokerHelper.asIterator(self), value);
2790 }
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802 public static Number count(long[] self, Object value) {
2803 return count(InvokerHelper.asIterator(self), value);
2804 }
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816 public static Number count(short[] self, Object value) {
2817 return count(InvokerHelper.asIterator(self), value);
2818 }
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830 public static Number count(char[] self, Object value) {
2831 return count(InvokerHelper.asIterator(self), value);
2832 }
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844 public static Number count(boolean[] self, Object value) {
2845 return count(InvokerHelper.asIterator(self), value);
2846 }
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858 public static Number count(double[] self, Object value) {
2859 return count(InvokerHelper.asIterator(self), value);
2860 }
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872 public static Number count(float[] self, Object value) {
2873 return count(InvokerHelper.asIterator(self), value);
2874 }
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886 public static Number count(byte[] self, Object value) {
2887 return count(InvokerHelper.asIterator(self), value);
2888 }
2889
2890
2891
2892
2893
2894
2895 @Deprecated
2896 public static <T> List<T> toList(Collection<T> self) {
2897 List<T> answer = new ArrayList<T>(self.size());
2898 answer.addAll(self);
2899 return answer;
2900 }
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910 public static <T> List<T> toList(Iterator<T> self) {
2911 List<T> answer = new ArrayList<T>();
2912 while (self.hasNext()) {
2913 answer.add(self.next());
2914 }
2915 return answer;
2916 }
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931 public static <T> List<T> toList(Iterable<T> self) {
2932 return toList(self.iterator());
2933 }
2934
2935
2936
2937
2938
2939
2940
2941
2942 public static <T> List<T> toList(Enumeration<T> self) {
2943 List<T> answer = new ArrayList<T>();
2944 while (self.hasMoreElements()) {
2945 answer.add(self.nextElement());
2946 }
2947 return answer;
2948 }
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962 public static <T> List<List<T>> collate(Iterable<T> self, int size) {
2963 return collate(self, size, true);
2964 }
2965
2966
2967
2968
2969
2970
2971 @Deprecated
2972 public static <T> List<List<T>> collate( List<T> self, int size ) {
2973 return collate((Iterable<T>) self, size) ;
2974 }
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990 public static <T> List<List<T>> collate(Iterable<T> self, int size, int step) {
2991 return collate(self, size, step, true);
2992 }
2993
2994
2995
2996
2997
2998
2999 @Deprecated
3000 public static <T> List<List<T>> collate( List<T> self, int size, int step ) {
3001 return collate((Iterable<T>) self, size, step) ;
3002 }
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018 public static <T> List<List<T>> collate(Iterable<T> self, int size, boolean keepRemainder) {
3019 return collate(self, size, size, keepRemainder);
3020 }
3021
3022
3023
3024
3025
3026
3027 @Deprecated
3028 public static <T> List<List<T>> collate( List<T> self, int size, boolean keepRemainder ) {
3029 return collate((Iterable<T>) self, size, keepRemainder) ;
3030 }
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051 public static <T> List<List<T>> collate(Iterable<T> self, int size, int step, boolean keepRemainder) {
3052 List<T> selfList = asList(self);
3053 List<List<T>> answer = new ArrayList<List<T>>();
3054 if (size <= 0 || selfList.size() == 0) {
3055 answer.add(selfList);
3056 } else {
3057 for (int pos = 0; pos < selfList.size() && pos > -1; pos += step) {
3058 if (!keepRemainder && pos > selfList.size() - size) {
3059 break ;
3060 }
3061 List<T> element = new ArrayList<T>() ;
3062 for (int offs = pos; offs < pos + size && offs < selfList.size(); offs++) {
3063 element.add(selfList.get(offs));
3064 }
3065 answer.add( element ) ;
3066 }
3067 }
3068 return answer ;
3069 }
3070
3071
3072
3073
3074
3075
3076 @Deprecated
3077 public static <T> List<List<T>> collate( List<T> self, int size, int step, boolean keepRemainder ) {
3078 return collate((Iterable<T>) self, size, step, keepRemainder);
3079 }
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094 public static <T> List<T> collect(Object self, Closure<T> transform) {
3095 return (List<T>) collect(self, new ArrayList<T>(), transform);
3096 }
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108 public static Collection collect(Object self) {
3109 return collect(self, Closure.IDENTITY);
3110 }
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122 public static <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform) {
3123 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); ) {
3124 collector.add(transform.call(iter.next()));
3125 }
3126 return collector;
3127 }
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139 public static <S,T> List<T> collect(Collection<S> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> transform) {
3140 return (List<T>) collect(self, new ArrayList<T>(self.size()), transform);
3141 }
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153 public static <T> List<T> collect(Collection<T> self) {
3154 return (List<T>) collect(self, Closure.IDENTITY);
3155 }
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168 public static <T,E> Collection<T> collect(Collection<E> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) {
3169 for (E item : self) {
3170 collector.add(transform.call(item));
3171 if (transform.getDirective() == Closure.DONE) {
3172 break;
3173 }
3174 }
3175 return collector;
3176 }
3177
3178
3179
3180
3181
3182
3183
3184 @Deprecated
3185 public static List collectAll(Collection self, Closure transform) {
3186 return collectNested(self, transform);
3187 }
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202 public static List collectNested(Collection self, Closure transform) {
3203 return (List) collectNested((Iterable) self, new ArrayList(self.size()), transform);
3204 }
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219 public static List collectNested(Iterable self, Closure transform) {
3220 return (List) collectNested(self, new ArrayList(), transform);
3221 }
3222
3223
3224
3225
3226
3227
3228
3229 @Deprecated
3230 public static Collection collectAll(Collection self, Collection collector, Closure transform) {
3231 return collectNested((Iterable)self, collector, transform);
3232 }
3233
3234
3235
3236
3237
3238
3239 @Deprecated
3240 public static Collection collectNested(Collection self, Collection collector, Closure transform) {
3241 return collectNested((Iterable)self, collector, transform);
3242 }
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260 public static Collection collectNested(Iterable self, Collection collector, Closure transform) {
3261 for (Object item : self) {
3262 if (item instanceof Collection) {
3263 Collection c = (Collection) item;
3264 collector.add(collectNested((Iterable)c, createSimilarCollection(collector, c.size()), transform));
3265 } else {
3266 collector.add(transform.call(item));
3267 }
3268 if (transform.getDirective() == Closure.DONE) {
3269 break;
3270 }
3271 }
3272 return collector;
3273 }
3274
3275
3276
3277
3278
3279
3280 @Deprecated
3281 public static <T,E> List<T> collectMany(Collection<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<Collection<? extends T>> projection) {
3282 return collectMany((Iterable)self, projection);
3283 }
3284
3285
3286
3287
3288
3289
3290 @Deprecated
3291 public static <T,E> Collection<T> collectMany(Collection<E> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<Collection<? extends T>> projection) {
3292 return collectMany((Iterable)self, collector, projection);
3293 }
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319 public static <T,E> List<T> collectMany(Iterable<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<Collection<? extends T>> projection) {
3320 return (List<T>) collectMany(self, new ArrayList<T>(), projection);
3321 }
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343 public static <T,E> Collection<T> collectMany(Iterable<E> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<Collection<? extends T>> projection) {
3344 for (E next : self) {
3345 collector.addAll(projection.call(next));
3346 }
3347 return collector;
3348 }
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366 public static <T,K,V> Collection<T> collectMany(Map<K, V> self, Collection<T> collector, @ClosureParams(MapEntryOrKeyValue.class) Closure<Collection<? extends T>> projection) {
3367 for (Map.Entry<K, V> entry : self.entrySet()) {
3368 collector.addAll(callClosureForMapEntry(projection, entry));
3369 }
3370 return collector;
3371 }
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388 public static <T,K,V> Collection<T> collectMany(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<Collection<? extends T>> projection) {
3389 return collectMany(self, new ArrayList<T>(), projection);
3390 }
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407 @SuppressWarnings("unchecked")
3408 public static <T,E> List<T> collectMany(E[] self, @ClosureParams(FirstParam.Component.class) Closure<Collection<? extends T>> projection) {
3409 return collectMany((Iterable<E>)toList(self), projection);
3410 }
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427 @SuppressWarnings("unchecked")
3428 public static <T,E> List<T> collectMany(Iterator<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<Collection<? extends T>> projection) {
3429 return collectMany((Iterable)toList(self), projection);
3430 }
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444 public static <T,K,V> Collection<T> collect(Map<K, V> self, Collection<T> collector, @ClosureParams(MapEntryOrKeyValue.class) Closure<? extends T> transform) {
3445 for (Map.Entry<K, V> entry : self.entrySet()) {
3446 collector.add(callClosureForMapEntry(transform, entry));
3447 }
3448 return collector;
3449 }
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462 public static <T,K,V> List<T> collect(Map<K,V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<T> transform) {
3463 return (List<T>) collect(self, new ArrayList<T>(self.size()), transform);
3464 }
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487 public static <K, V, S, T> Map<K, V> collectEntries(Map<S, T> self, Map<K, V> collector, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> transform) {
3488 for (Map.Entry<S, T> entry : self.entrySet()) {
3489 addEntry(collector, callClosureForMapEntry(transform, entry));
3490 }
3491 return collector;
3492 }
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514 public static <K,V> Map<?, ?> collectEntries(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> transform) {
3515 return collectEntries(self, createSimilarMap(self), transform);
3516 }
3517
3518
3519
3520
3521
3522
3523 @Deprecated
3524 public static <K, V> Map<K, V> collectEntries(Collection<?> self, Closure<?> transform) {
3525 return collectEntries((Iterable)self, new LinkedHashMap<K, V>(), transform);
3526 }
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538 public static <K, V, E> Map<K, V> collectEntries(Iterator<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
3539 return collectEntries(self, new LinkedHashMap<K, V>(), transform);
3540 }
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563 public static <K,V,E> Map<K, V> collectEntries(Iterable<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
3564 return collectEntries(self.iterator(), transform);
3565 }
3566
3567
3568
3569
3570
3571
3572 @Deprecated
3573 public static <K, V> Map<K, V> collectEntries(Collection<?> self) {
3574 return collectEntries((Iterable)self, new LinkedHashMap<K, V>(), Closure.IDENTITY);
3575 }
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585 public static <K, V> Map<K, V> collectEntries(Iterator<?> self) {
3586 return collectEntries(self, Closure.IDENTITY);
3587 }
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605 public static <K, V> Map<K, V> collectEntries(Iterable<?> self) {
3606 return collectEntries(self.iterator());
3607 }
3608
3609
3610
3611
3612
3613
3614 @Deprecated
3615 public static <K, V> Map<K, V> collectEntries(Collection<?> self, Map<K, V> collector, Closure<?> transform) {
3616 return collectEntries((Iterable<?>)self, collector, transform);
3617 }
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629 public static <K, V, E> Map<K, V> collectEntries(Iterator<E> self, Map<K, V> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
3630 while (self.hasNext()) {
3631 E next = self.next();
3632 addEntry(collector, transform.call(next));
3633 }
3634 return collector;
3635 }
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660 public static <K, V, E> Map<K, V> collectEntries(Iterable<E> self, Map<K, V> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> transform) {
3661 return collectEntries(self.iterator(), collector, transform);
3662 }
3663
3664
3665
3666
3667
3668
3669 @Deprecated
3670 public static <K, V> Map<K, V> collectEntries(Collection<?> self, Map<K, V> collector) {
3671 return collectEntries((Iterable<?>)self, collector, Closure.IDENTITY);
3672 }
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684 public static <K, V> Map<K, V> collectEntries(Iterator<?> self, Map<K, V> collector) {
3685 return collectEntries(self, collector, Closure.IDENTITY);
3686 }
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698 public static <K, V> Map<K, V> collectEntries(Iterable<?> self, Map<K, V> collector) {
3699 return collectEntries(self.iterator(), collector);
3700 }
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726 @SuppressWarnings("unchecked")
3727 public static <K, V, E> Map<K, V> collectEntries(E[] self, Map<K, V> collector, @ClosureParams(FirstParam.Component.class) Closure<?> transform) {
3728 return collectEntries((Iterable)toList(self), collector, transform);
3729 }
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740 public static <K, V, E> Map<K, V> collectEntries(E[] self, Map<K, V> collector) {
3741 return collectEntries(self, collector, Closure.IDENTITY);
3742 }
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766 public static <K, V, E> Map<K, V> collectEntries(E[] self, @ClosureParams(FirstParam.Component.class) Closure<?> transform) {
3767 return collectEntries((Iterable)toList(self), new LinkedHashMap<K, V>(), transform);
3768 }
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778 public static <K, V, E> Map<K, V> collectEntries(E[] self) {
3779 return collectEntries(self, Closure.IDENTITY);
3780 }
3781
3782 private static <K, V> void addEntry(Map<K, V> result, Object newEntry) {
3783 if (newEntry instanceof Map) {
3784 leftShift(result, (Map)newEntry);
3785 } else if (newEntry instanceof List) {
3786 List list = (List) newEntry;
3787
3788 Object key = list.size() == 0 ? null : list.get(0);
3789 Object value = list.size() <= 1 ? null : list.get(1);
3790 leftShift(result, new MapEntry(key, value));
3791 } else {
3792
3793
3794
3795 leftShift(result, asType(newEntry, Map.Entry.class));
3796 }
3797 }
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807 public static Object find(Object self, Closure closure) {
3808 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
3809 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
3810 Object value = iter.next();
3811 if (bcw.call(value)) {
3812 return value;
3813 }
3814 }
3815 return null;
3816 }
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832 public static Object find(Object self) {
3833 return find(self, Closure.IDENTITY);
3834 }
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845 public static Object findResult(Object self, Object defaultResult, Closure closure) {
3846 Object result = findResult(self, closure);
3847 if (result == null) return defaultResult;
3848 return result;
3849 }
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859 public static Object findResult(Object self, Closure closure) {
3860 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) {
3861 Object value = iter.next();
3862 Object result = closure.call(value);
3863 if (result != null) {
3864 return result;
3865 }
3866 }
3867 return null;
3868 }
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881 public static <T> T find(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
3882 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
3883 for (T value : self) {
3884 if (bcw.call(value)) {
3885 return value;
3886 }
3887 }
3888 return null;
3889 }
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905 public static <T> T find(T[] self, @ClosureParams(FirstParam.Component.class) Closure condition) {
3906 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
3907 for (T element : self) {
3908 if (bcw.call(element)) {
3909 return element;
3910 }
3911 }
3912 return null;
3913 }
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929 public static <T> T find(Collection<T> self) {
3930 return find(self, Closure.IDENTITY);
3931 }
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950 public static <T, U extends T, V extends T,E> T findResult(Collection<E> self, U defaultResult, @ClosureParams(FirstParam.FirstGenericType.class) Closure<V> closure) {
3951 T result = findResult(self, closure);
3952 if (result == null) return defaultResult;
3953 return result;
3954 }
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971 public static <T,U> T findResult(Collection<U> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> closure) {
3972 for (Object value : self) {
3973 T result = closure.call(value);
3974 if (result != null) {
3975 return result;
3976 }
3977 }
3978 return null;
3979 }
3980
3981
3982
3983
3984
3985
3986 @Deprecated
3987 public static <T,U> Collection<T> findResults(Collection<U> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> filteringTransform) {
3988 return findResults((Iterable<?>)self, filteringTransform);
3989 }
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007 public static <T,U> Collection<T> findResults(Iterable<U> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> filteringTransform) {
4008 List<T> result = new ArrayList<T>();
4009 for (Object value : self) {
4010 T transformed = filteringTransform.call(value);
4011 if (transformed != null) {
4012 result.add(transformed);
4013 }
4014 }
4015 return result;
4016 }
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036 public static <T,K,V> Collection<T> findResults(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<T> filteringTransform) {
4037 List<T> result = new ArrayList<T>();
4038 for (Map.Entry<K, V> entry : self.entrySet()) {
4039 T transformed = callClosureForMapEntry(filteringTransform, entry);
4040 if (transformed != null) {
4041 result.add(transformed);
4042 }
4043 }
4044 return result;
4045 }
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058 public static <K, V> Map.Entry<K, V> find(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> closure) {
4059 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
4060 for (Map.Entry<K, V> entry : self.entrySet()) {
4061 if (bcw.callForMap(entry)) {
4062 return entry;
4063 }
4064 }
4065 return null;
4066 }
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084 public static <T, U extends T, V extends T,A,B> T findResult(Map<A, B> self, U defaultResult, @ClosureParams(MapEntryOrKeyValue.class) Closure<V> closure) {
4085 T result = findResult(self, closure);
4086 if (result == null) return defaultResult;
4087 return result;
4088 }
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105 public static <T,K,V> T findResult(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<T> closure) {
4106 for (Map.Entry<K, V> entry : self.entrySet()) {
4107 T result = callClosureForMapEntry(closure, entry);
4108 if (result != null) {
4109 return result;
4110 }
4111 }
4112 return null;
4113 }
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124 public static <T> Set<T> findAll(Set<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4125 return (Set<T>) findAll((Collection<T>) self, closure);
4126 }
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137 public static <T> List<T> findAll(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4138 return (List<T>) findAll((Collection<T>) self, closure);
4139 }
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150 public static <T> Collection<T> findAll(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4151 Collection<T> answer = createSimilarCollection(self);
4152 Iterator<T> iter = self.iterator();
4153 return findAll(closure, answer, iter);
4154 }
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168 public static <T> Collection<T> findAll(T[] self, @ClosureParams(FirstParam.Component.class) Closure condition) {
4169 Collection<T> answer = new ArrayList<T>();
4170 return findAll(condition, answer, new ArrayIterator<T>(self));
4171 }
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187 public static <T> Set<T> findAll(Set<T> self) {
4188 return findAll(self, Closure.IDENTITY);
4189 }
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205 public static <T> List<T> findAll(List<T> self) {
4206 return findAll(self, Closure.IDENTITY);
4207 }
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223 public static <T> Collection<T> findAll(Collection<T> self) {
4224 return findAll(self, Closure.IDENTITY);
4225 }
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241 public static <T> Collection<T> findAll(T[] self) {
4242 return findAll(self, Closure.IDENTITY);
4243 }
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253 public static Collection findAll(Object self, Closure closure) {
4254 List answer = new ArrayList();
4255 Iterator iter = InvokerHelper.asIterator(self);
4256 return findAll(closure, answer, iter);
4257 }
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273 public static Collection findAll(Object self) {
4274 return findAll(self, Closure.IDENTITY);
4275 }
4276
4277 private static <T> Collection<T> findAll(Closure closure, Collection<T> answer, Iterator<? extends T> iter) {
4278 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
4279 while (iter.hasNext()) {
4280 T value = iter.next();
4281 if (bcw.call(value)) {
4282 answer.add(value);
4283 }
4284 }
4285 return answer;
4286 }
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297 public static boolean contains(Iterable self, Object item) {
4298 for (Object e : self) {
4299 if (item == null ? e == null : item.equals(e)) {
4300 return true;
4301 }
4302 }
4303 return false;
4304 }
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317 public static boolean containsAll(Iterable self, Object[] items) {
4318 return asCollection(self).containsAll(Arrays.asList(items));
4319 }
4320
4321
4322
4323
4324
4325
4326 @Deprecated
4327 public static boolean containsAll(Collection self, Object[] items) {
4328 return self.containsAll(Arrays.asList(items));
4329 }
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344 public static boolean removeAll(Collection self, Object[] items) {
4345 Collection pickFrom = new TreeSet(new NumberAwareComparator());
4346 pickFrom.addAll(Arrays.asList(items));
4347 return self.removeAll(pickFrom);
4348 }
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364 public static boolean retainAll(Collection self, Object[] items) {
4365 Collection pickFrom = new TreeSet(new NumberAwareComparator());
4366 pickFrom.addAll(Arrays.asList(items));
4367 return self.retainAll(pickFrom);
4368 }
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384 public static <T> boolean retainAll(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
4385 Iterator iter = InvokerHelper.asIterator(self);
4386 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
4387 boolean result = false;
4388 while (iter.hasNext()) {
4389 Object value = iter.next();
4390 if (!bcw.call(value)) {
4391 iter.remove();
4392 result = true;
4393 }
4394 }
4395 return result;
4396 }
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411 public static <T> boolean removeAll(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
4412 Iterator iter = InvokerHelper.asIterator(self);
4413 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
4414 boolean result = false;
4415 while (iter.hasNext()) {
4416 Object value = iter.next();
4417 if (bcw.call(value)) {
4418 iter.remove();
4419 result = true;
4420 }
4421 }
4422 return result;
4423 }
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439 public static <T> boolean addAll(Collection<T> self, T[] items) {
4440 return self.addAll(Arrays.asList(items));
4441 }
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463 public static <T> boolean addAll(List<T> self, int index, T[] items) {
4464 return self.addAll(index, Arrays.asList(items));
4465 }
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477 public static Collection split(Object self, Closure closure) {
4478 List accept = new ArrayList();
4479 List reject = new ArrayList();
4480 return split(closure, accept, reject, InvokerHelper.asIterator(self));
4481 }
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496 public static <T> Collection<Collection<T>> split(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4497 Collection<T> accept = createSimilarCollection(self);
4498 Collection<T> reject = createSimilarCollection(self);
4499 Iterator<T> iter = self.iterator();
4500 return split(closure, accept, reject, iter);
4501 }
4502
4503 private static <T> Collection<Collection<T>> split(Closure closure, Collection<T> accept, Collection<T> reject, Iterator<T> iter) {
4504 List<Collection<T>> answer = new ArrayList<Collection<T>>();
4505 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
4506 while (iter.hasNext()) {
4507 T value = iter.next();
4508 if (bcw.call(value)) {
4509 accept.add(value);
4510 } else {
4511 reject.add(value);
4512 }
4513 }
4514 answer.add(accept);
4515 answer.add(reject);
4516 return answer;
4517 }
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532 @SuppressWarnings("unchecked")
4533 public static <T> List<List<T>> split(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4534 return (List<List<T>>) (List<?>) split((Collection<T>) self, closure);
4535 }
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550 @SuppressWarnings("unchecked")
4551 public static <T> List<Set<T>> split(Set<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
4552 return (List<Set<T>>) (List<?>) split((Collection<T>) self, closure);
4553 }
4554
4555
4556
4557
4558
4559
4560 @Deprecated
4561 public static List combinations(Collection self) {
4562 return combinations((Iterable)self);
4563 }
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578 public static List combinations(Iterable self) {
4579 return GroovyCollections.combinations(self);
4580 }
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595 public static List combinations(Iterable self, Closure<?> function) {
4596 return collect(GroovyCollections.combinations(self), function);
4597 }
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610 public static void eachCombination(Iterable self, Closure<?> function) {
4611 each(GroovyCollections.combinations(self), function);
4612 }
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625 public static <T> Set<List<T>> subsequences(List<T> self) {
4626 return GroovyCollections.subsequences(self);
4627 }
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640 public static <T> Set<List<T>> permutations(Iterable<T> self) {
4641 Set<List<T>> ans = new HashSet<List<T>>();
4642 PermutationGenerator<T> generator = new PermutationGenerator<T>(self);
4643 while (generator.hasNext()) {
4644 ans.add(generator.next());
4645 }
4646 return ans;
4647 }
4648
4649
4650
4651
4652
4653
4654 @Deprecated
4655 public static <T> Set<List<T>> permutations(List<T> self) {
4656 return permutations((Iterable<T>) self);
4657 }
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672 public static <T,V> List<V> permutations(Iterable<T> self, Closure<V> function) {
4673 return collect(permutations(self),function);
4674 }
4675
4676
4677
4678
4679
4680
4681 @Deprecated
4682 public static <T, V> List<V> permutations(List<T> self, Closure<V> function) {
4683 return permutations((Iterable<T>) self, function);
4684 }
4685
4686
4687
4688
4689
4690
4691 @Deprecated
4692 public static <T> Iterator<List<T>> eachPermutation(Collection<T> self, Closure closure) {
4693 return eachPermutation((Iterable<T>) self, closure);
4694 }
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709 public static <T> Iterator<List<T>> eachPermutation(Iterable<T> self, Closure closure) {
4710 Iterator<List<T>> generator = new PermutationGenerator<T>(self);
4711 while (generator.hasNext()) {
4712 closure.call(generator.next());
4713 }
4714 return generator;
4715 }
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734 public static List transpose(List self) {
4735 return GroovyCollections.transpose(self);
4736 }
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761 public static <K, V> Map<K, V> findAll(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure closure) {
4762 Map<K, V> answer = createSimilarMap(self);
4763 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
4764 for (Map.Entry<K, V> entry : self.entrySet()) {
4765 if (bcw.callForMap(entry)) {
4766 answer.put(entry.getKey(), entry.getValue());
4767 }
4768 }
4769 return answer;
4770 }
4771
4772
4773
4774
4775
4776
4777 @Deprecated
4778 public static <K, T> Map<K, List<T>> groupBy(Collection<T> self, Closure<K> closure) {
4779 return groupBy((Iterable<T>)self, closure);
4780 }
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798 public static <K, T> Map<K, List<T>> groupBy(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<K> closure) {
4799 Map<K, List<T>> answer = new LinkedHashMap<K, List<T>>();
4800 for (T element : self) {
4801 K value = closure.call(element);
4802 groupAnswer(answer, element, value);
4803 }
4804 return answer;
4805 }
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825 public static <K, T> Map<K, List<T>> groupBy(T[] self, @ClosureParams(FirstParam.Component.class) Closure<K> closure) {
4826 return groupBy((Iterable<T>)Arrays.asList(self), closure);
4827 }
4828
4829
4830
4831
4832
4833
4834 @Deprecated
4835 public static Map groupBy(Collection self, Object... closures) {
4836 return groupBy((Iterable)self, closures);
4837 }
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869 public static Map groupBy(Iterable self, Object... closures) {
4870 final Closure head = closures.length == 0 ? Closure.IDENTITY : (Closure) closures[0];
4871
4872 @SuppressWarnings("unchecked")
4873 Map<Object, List> first = groupBy(self, head);
4874 if (closures.length < 2)
4875 return first;
4876
4877 final Object[] tail = new Object[closures.length - 1];
4878 System.arraycopy(closures, 1, tail, 0, closures.length - 1);
4879
4880
4881 Map<Object, Map> acc = new LinkedHashMap<Object, Map>();
4882 for (Map.Entry<Object, List> item : first.entrySet()) {
4883 acc.put(item.getKey(), groupBy((Iterable)item.getValue(), tail));
4884 }
4885
4886 return acc;
4887 }
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900 public static Map groupBy(Object[] self, Object... closures) {
4901 return groupBy((Iterable)Arrays.asList(self), closures);
4902 }
4903
4904
4905
4906
4907
4908
4909 @Deprecated
4910 public static Map groupBy(Collection self, List<Closure> closures) {
4911 return groupBy((Iterable)self, closures);
4912 }
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948 public static Map groupBy(Iterable self, List<Closure> closures) {
4949 return groupBy(self, closures.toArray());
4950 }
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963 public static Map groupBy(Object[] self, List<Closure> closures) {
4964 return groupBy((Iterable)Arrays.asList(self), closures);
4965 }
4966
4967
4968
4969
4970
4971
4972 @Deprecated
4973 public static <K> Map<K, Integer> countBy(Collection self, Closure<K> closure) {
4974 return countBy((Iterable) self, closure);
4975 }
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992 public static <K,E> Map<K, Integer> countBy(Iterable<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<K> closure) {
4993 return countBy(self.iterator(), closure);
4994 }
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012 public static <K,E> Map<K, Integer> countBy(E[] self, @ClosureParams(FirstParam.Component.class) Closure<K> closure) {
5013 return countBy((Iterable)Arrays.asList(self), closure);
5014 }
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032 public static <K,E> Map<K, Integer> countBy(Iterator<E> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<K> closure) {
5033 Map<K, Integer> answer = new LinkedHashMap<K, Integer>();
5034 while (self.hasNext()) {
5035 K value = closure.call(self.next());
5036 countAnswer(answer, value);
5037 }
5038 return answer;
5039 }
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059 public static <G, K, V> Map<G, List<Map.Entry<K, V>>> groupEntriesBy(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<G> closure) {
5060 final Map<G, List<Map.Entry<K, V>>> answer = new LinkedHashMap<G, List<Map.Entry<K, V>>>();
5061 for (Map.Entry<K, V> entry : self.entrySet()) {
5062 G value = callClosureForMapEntry(closure, entry);
5063 groupAnswer(answer, entry, value);
5064 }
5065 return answer;
5066 }
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089 public static <G, K, V> Map<G, Map<K, V>> groupBy(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<G> closure) {
5090 final Map<G, List<Map.Entry<K, V>>> initial = groupEntriesBy(self, closure);
5091 final Map<G, Map<K, V>> answer = new LinkedHashMap<G, Map<K, V>>();
5092 for (Map.Entry<G, List<Map.Entry<K, V>>> outer : initial.entrySet()) {
5093 G key = outer.getKey();
5094 List<Map.Entry<K, V>> entries = outer.getValue();
5095 Map<K, V> target = createSimilarMap(self);
5096 putAll(target, entries);
5097 answer.put(key, target);
5098 }
5099 return answer;
5100 }
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125 public static Map<Object, Map> groupBy(Map self, Object... closures) {
5126 @SuppressWarnings("unchecked")
5127 final Closure<Object> head = closures.length == 0 ? Closure.IDENTITY : (Closure) closures[0];
5128
5129 @SuppressWarnings("unchecked")
5130 Map<Object, Map> first = groupBy(self, head);
5131 if (closures.length < 2)
5132 return first;
5133
5134 final Object[] tail = new Object[closures.length - 1];
5135 System.arraycopy(closures, 1, tail, 0, closures.length - 1);
5136
5137 Map<Object, Map> acc = new LinkedHashMap<Object, Map>();
5138 for (Map.Entry<Object, Map> item: first.entrySet()) {
5139 acc.put(item.getKey(), groupBy(item.getValue(), tail));
5140 }
5141
5142 return acc;
5143 }
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168 public static Map<Object, Map> groupBy(Map self, List<Closure> closures) {
5169 return groupBy(self, closures.toArray());
5170 }
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189 public static <K,U,V> Map<K, Integer> countBy(Map<U,V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<K> closure) {
5190 Map<K, Integer> answer = new LinkedHashMap<K, Integer>();
5191 for (Object entry : self.entrySet()) {
5192 countAnswer(answer, callClosureForMapEntry(closure, (Map.Entry) entry));
5193 }
5194 return answer;
5195 }
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205 protected static <K, T> void groupAnswer(final Map<K, List<T>> answer, T element, K value) {
5206 if (answer.containsKey(value)) {
5207 answer.get(value).add(element);
5208 } else {
5209 List<T> groupedElements = new ArrayList<T>();
5210 groupedElements.add(element);
5211 answer.put(value, groupedElements);
5212 }
5213 }
5214
5215 private static <T> void countAnswer(final Map<T, Integer> answer, T mappedKey) {
5216 if (!answer.containsKey(mappedKey)) {
5217 answer.put(mappedKey, 0);
5218 }
5219 int current = answer.get(mappedKey);
5220 answer.put(mappedKey, current + 1);
5221 }
5222
5223
5224 protected static <T> T callClosureForMapEntry(Closure<T> closure, Map.Entry entry) {
5225 if (closure.getMaximumNumberOfParameters() == 2) {
5226 return closure.call(new Object[]{entry.getKey(), entry.getValue()});
5227 }
5228 return closure.call(entry);
5229 }
5230
5231
5232 protected static <T> T callClosureForLine(Closure<T> closure, String line, int counter) {
5233 if (closure.getMaximumNumberOfParameters() == 2) {
5234 return closure.call(new Object[]{line, counter});
5235 }
5236 return closure.call(line);
5237 }
5238
5239
5240 protected static <T> T callClosureForMapEntryAndCounter(Closure<T> closure, Map.Entry entry, int counter) {
5241 if (closure.getMaximumNumberOfParameters() == 3) {
5242 return closure.call(new Object[]{entry.getKey(), entry.getValue(), counter});
5243 }
5244 if (closure.getMaximumNumberOfParameters() == 2) {
5245 return closure.call(new Object[]{entry, counter});
5246 }
5247 return closure.call(entry);
5248 }
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268 public static <T, V extends T> T inject(Collection<T> self, @ClosureParams(value=FromString.class,options="V,T") Closure<V> closure ) {
5269 if( self.isEmpty() ) {
5270 throw new NoSuchElementException( "Cannot call inject() on an empty collection without passing an initial value." ) ;
5271 }
5272 Iterator<T> iter = self.iterator();
5273 T head = iter.next();
5274 Collection<T> tail = tail(self);
5275 if (!tail.iterator().hasNext()) {
5276 return head;
5277 }
5278
5279 return (T) inject((Collection) tail, head, closure);
5280 }
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322 public static <E, T, U extends T, V extends T> T inject(Collection<E> self, U initialValue, @ClosureParams(value=FromString.class,options="U,E") Closure<V> closure) {
5323
5324 return (T) inject((Iterator) self.iterator(), initialValue, closure);
5325 }
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349 public static <K, V, T, U extends T, W extends T> T inject(Map<K, V> self, U initialValue, @ClosureParams(value=FromString.class,options={"U,Map.Entry<K,V>","U,K,V"}) Closure<W> closure) {
5350 T value = initialValue;
5351 for (Map.Entry<K, V> entry : self.entrySet()) {
5352 if (closure.getMaximumNumberOfParameters() == 3) {
5353 value = closure.call(value, entry.getKey(), entry.getValue());
5354 } else {
5355 value = closure.call(value, entry);
5356 }
5357 }
5358 return value;
5359 }
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376 public static <E,T, U extends T, V extends T> T inject(Iterator<E> self, U initialValue, @ClosureParams(value=FromString.class,options="U,E") Closure<V> closure) {
5377 T value = initialValue;
5378 Object[] params = new Object[2];
5379 while (self.hasNext()) {
5380 Object item = self.next();
5381 params[0] = value;
5382 params[1] = item;
5383 value = closure.call(params);
5384 }
5385 return value;
5386 }
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402 public static <T, V extends T> T inject(Object self, Closure<V> closure) {
5403 Iterator iter = InvokerHelper.asIterator(self);
5404 if( !iter.hasNext() ) {
5405 throw new NoSuchElementException( "Cannot call inject() over an empty iterable without passing an initial value." ) ;
5406 }
5407 Object initialValue = iter.next() ;
5408 return (T) inject(iter, initialValue, closure);
5409 }
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425 public static <T, U extends T, V extends T> T inject(Object self, U initialValue, Closure<V> closure) {
5426 Iterator iter = InvokerHelper.asIterator(self);
5427 return (T) inject(iter, initialValue, closure);
5428 }
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442 public static <E,T, V extends T> T inject(E[] self, @ClosureParams(value=FromString.class,options="E,E") Closure<V> closure) {
5443 return inject( (Object)self, closure ) ;
5444 }
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460 public static <E, T, U extends T, V extends T> T inject(E[] self, U initialValue, @ClosureParams(value=FromString.class,options="U,E") Closure<V> closure) {
5461 Object[] params = new Object[2];
5462 T value = initialValue;
5463 for (Object next : self) {
5464 params[0] = value;
5465 params[1] = next;
5466 value = closure.call(params);
5467 }
5468 return value;
5469 }
5470
5471
5472
5473
5474
5475
5476 @Deprecated
5477 public static Object sum(Collection self) {
5478 return sum((Iterable)self);
5479 }
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490 public static Object sum(Iterable self) {
5491 return sum(self, null, true);
5492 }
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503 public static Object sum(Object[] self) {
5504 return sum(toList(self), null, true);
5505 }
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516 public static Object sum(Iterator<Object> self) {
5517 return sum(toList(self), null, true);
5518 }
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528 public static byte sum(byte[] self) {
5529 return sum(self, (byte) 0);
5530 }
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540 public static short sum(short[] self) {
5541 return sum(self, (short) 0);
5542 }
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552 public static int sum(int[] self) {
5553 return sum(self, 0);
5554 }
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564 public static long sum(long[] self) {
5565 return sum(self, (long) 0);
5566 }
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576 public static char sum(char[] self) {
5577 return sum(self, (char) 0);
5578 }
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588 public static float sum(float[] self) {
5589 return sum(self, (float) 0);
5590 }
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600 public static double sum(double[] self) {
5601 return sum(self, (double) 0);
5602 }
5603
5604
5605
5606
5607
5608
5609 @Deprecated
5610 public static Object sum(Collection self, Object initialValue) {
5611 return sum(self, initialValue, false);
5612 }
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625 public static Object sum(Iterable self, Object initialValue) {
5626 return sum(self, initialValue, false);
5627 }
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637 public static Object sum(Object[] self, Object initialValue) {
5638 return sum(toList(self), initialValue, false);
5639 }
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651 public static Object sum(Iterator<Object> self, Object initialValue) {
5652 return sum(toList(self), initialValue, false);
5653 }
5654
5655 private static Object sum(Iterable self, Object initialValue, boolean first) {
5656 Object result = initialValue;
5657 Object[] param = new Object[1];
5658 for (Object next : self) {
5659 param[0] = next;
5660 if (first) {
5661 result = param[0];
5662 first = false;
5663 continue;
5664 }
5665 MetaClass metaClass = InvokerHelper.getMetaClass(result);
5666 result = metaClass.invokeMethod(result, "plus", param);
5667 }
5668 return result;
5669 }
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680 public static byte sum(byte[] self, byte initialValue) {
5681 byte s = initialValue;
5682 for (byte v : self) {
5683 s += v;
5684 }
5685 return s;
5686 }
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697 public static short sum(short[] self, short initialValue) {
5698 short s = initialValue;
5699 for (short v : self) {
5700 s += v;
5701 }
5702 return s;
5703 }
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714 public static int sum(int[] self, int initialValue) {
5715 int s = initialValue;
5716 for (int v : self) {
5717 s += v;
5718 }
5719 return s;
5720 }
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731 public static long sum(long[] self, long initialValue) {
5732 long s = initialValue;
5733 for (long v : self) {
5734 s += v;
5735 }
5736 return s;
5737 }
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748 public static char sum(char[] self, char initialValue) {
5749 char s = initialValue;
5750 for (char v : self) {
5751 s += v;
5752 }
5753 return s;
5754 }
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765 public static float sum(float[] self, float initialValue) {
5766 float s = initialValue;
5767 for (float v : self) {
5768 s += v;
5769 }
5770 return s;
5771 }
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782 public static double sum(double[] self, double initialValue) {
5783 double s = initialValue;
5784 for (double v : self) {
5785 s += v;
5786 }
5787 return s;
5788 }
5789
5790
5791
5792
5793
5794
5795 @Deprecated
5796 public static Object sum(Collection self, Closure closure) {
5797 return sum((Iterable)self, closure);
5798 }
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812 public static Object sum(Iterable self, Closure closure) {
5813 return sum(self, null, closure, true);
5814 }
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827 public static Object sum(Object[] self, Closure closure) {
5828 return sum(toList(self), null, closure, true);
5829 }
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843 public static Object sum(Iterator<Object> self, Closure closure) {
5844 return sum(toList(self), null, closure, true);
5845 }
5846
5847
5848
5849
5850
5851
5852 @Deprecated
5853 public static Object sum(Collection self, Object initialValue, Closure closure) {
5854 return sum((Iterable)self, initialValue, closure);
5855 }
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870 public static Object sum(Iterable self, Object initialValue, Closure closure) {
5871 return sum(self, initialValue, closure, false);
5872 }
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886 public static Object sum(Object[] self, Object initialValue, Closure closure) {
5887 return sum(toList(self), initialValue, closure, false);
5888 }
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903 public static Object sum(Iterator<Object> self, Object initialValue, Closure closure) {
5904 return sum(toList(self), initialValue, closure, false);
5905 }
5906
5907 private static Object sum(Iterable self, Object initialValue, Closure closure, boolean first) {
5908 Object result = initialValue;
5909 Object[] closureParam = new Object[1];
5910 Object[] plusParam = new Object[1];
5911 for (Object next : self) {
5912 closureParam[0] = next;
5913 plusParam[0] = closure.call(closureParam);
5914 if (first) {
5915 result = plusParam[0];
5916 first = false;
5917 continue;
5918 }
5919 MetaClass metaClass = InvokerHelper.getMetaClass(result);
5920 result = metaClass.invokeMethod(result, "plus", plusParam);
5921 }
5922 return result;
5923 }
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936 public static String join(Iterator<Object> self, String separator) {
5937 return join((Iterable)toList(self), separator);
5938 }
5939
5940
5941
5942
5943
5944
5945 @Deprecated
5946 public static String join(Collection self, String separator) {
5947 return join((Iterable)self, separator);
5948 }
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960 public static String join(Iterable self, String separator) {
5961 StringBuilder buffer = new StringBuilder();
5962 boolean first = true;
5963
5964 if (separator == null) separator = "";
5965
5966 for (Object value : self) {
5967 if (first) {
5968 first = false;
5969 } else {
5970 buffer.append(separator);
5971 }
5972 buffer.append(InvokerHelper.toString(value));
5973 }
5974 return buffer.toString();
5975 }
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987 public static String join(Object[] self, String separator) {
5988 StringBuilder buffer = new StringBuilder();
5989 boolean first = true;
5990
5991 if (separator == null) separator = "";
5992
5993 for (Object next : self) {
5994 String value = InvokerHelper.toString(next);
5995 if (first) {
5996 first = false;
5997 } else {
5998 buffer.append(separator);
5999 }
6000 buffer.append(value);
6001 }
6002 return buffer.toString();
6003 }
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015 public static String join(boolean[] self, String separator) {
6016 StringBuilder buffer = new StringBuilder();
6017 boolean first = true;
6018
6019 if (separator == null) separator = "";
6020
6021 for (boolean next : self) {
6022 if (first) {
6023 first = false;
6024 } else {
6025 buffer.append(separator);
6026 }
6027 buffer.append(next);
6028 }
6029 return buffer.toString();
6030 }
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042 public static String join(byte[] self, String separator) {
6043 StringBuilder buffer = new StringBuilder();
6044 boolean first = true;
6045
6046 if (separator == null) separator = "";
6047
6048 for (byte next : self) {
6049 if (first) {
6050 first = false;
6051 } else {
6052 buffer.append(separator);
6053 }
6054 buffer.append(next);
6055 }
6056 return buffer.toString();
6057 }
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069 public static String join(char[] self, String separator) {
6070 StringBuilder buffer = new StringBuilder();
6071 boolean first = true;
6072
6073 if (separator == null) separator = "";
6074
6075 for (char next : self) {
6076 if (first) {
6077 first = false;
6078 } else {
6079 buffer.append(separator);
6080 }
6081 buffer.append(next);
6082 }
6083 return buffer.toString();
6084 }
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096 public static String join(double[] self, String separator) {
6097 StringBuilder buffer = new StringBuilder();
6098 boolean first = true;
6099
6100 if (separator == null) separator = "";
6101
6102 for (double next : self) {
6103 if (first) {
6104 first = false;
6105 } else {
6106 buffer.append(separator);
6107 }
6108 buffer.append(next);
6109 }
6110 return buffer.toString();
6111 }
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123 public static String join(float[] self, String separator) {
6124 StringBuilder buffer = new StringBuilder();
6125 boolean first = true;
6126
6127 if (separator == null) separator = "";
6128
6129 for (float next : self) {
6130 if (first) {
6131 first = false;
6132 } else {
6133 buffer.append(separator);
6134 }
6135 buffer.append(next);
6136 }
6137 return buffer.toString();
6138 }
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150 public static String join(int[] self, String separator) {
6151 StringBuilder buffer = new StringBuilder();
6152 boolean first = true;
6153
6154 if (separator == null) separator = "";
6155
6156 for (int next : self) {
6157 if (first) {
6158 first = false;
6159 } else {
6160 buffer.append(separator);
6161 }
6162 buffer.append(next);
6163 }
6164 return buffer.toString();
6165 }
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177 public static String join(long[] self, String separator) {
6178 StringBuilder buffer = new StringBuilder();
6179 boolean first = true;
6180
6181 if (separator == null) separator = "";
6182
6183 for (long next : self) {
6184 if (first) {
6185 first = false;
6186 } else {
6187 buffer.append(separator);
6188 }
6189 buffer.append(next);
6190 }
6191 return buffer.toString();
6192 }
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204 public static String join(short[] self, String separator) {
6205 StringBuilder buffer = new StringBuilder();
6206 boolean first = true;
6207
6208 if (separator == null) separator = "";
6209
6210 for (short next : self) {
6211 if (first) {
6212 first = false;
6213 } else {
6214 buffer.append(separator);
6215 }
6216 buffer.append(next);
6217 }
6218 return buffer.toString();
6219 }
6220
6221
6222
6223
6224
6225
6226 @Deprecated
6227 public static <T> T min(Collection<T> self) {
6228 return GroovyCollections.min(self);
6229 }
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240 public static <T> T min(Iterable<T> self) {
6241 return GroovyCollections.min(self);
6242 }
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253 public static <T> T min(Iterator<T> self) {
6254 return min((Iterable<T>)toList(self));
6255 }
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265 public static <T> T min(T[] self) {
6266 return min((Iterable<T>)toList(self));
6267 }
6268
6269
6270
6271
6272
6273
6274 @Deprecated
6275 public static <T> T min(Collection<T> self, Comparator<T> comparator) {
6276 return min((Iterable<T>) self, comparator);
6277 }
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288 public static <T> T min(Iterable<T> self, Comparator<T> comparator) {
6289 T answer = null;
6290 boolean first = true;
6291 for (T value : self) {
6292 if (first) {
6293 first = false;
6294 answer = value;
6295 } else if (comparator.compare(value, answer) < 0) {
6296 answer = value;
6297 }
6298 }
6299 return answer;
6300 }
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311 public static <T> T min(Iterator<T> self, Comparator<T> comparator) {
6312 return min((Iterable<T>)toList(self), comparator);
6313 }
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324 public static <T> T min(T[] self, Comparator<T> comparator) {
6325 return min((Iterable<T>)toList(self), comparator);
6326 }
6327
6328
6329
6330
6331
6332
6333 @Deprecated
6334 public static <T> T min(Collection<T> self, Closure closure) {
6335 return min((Iterable<T>)self, closure);
6336 }
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369 public static <T> T min(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
6370 int params = closure.getMaximumNumberOfParameters();
6371 if (params != 1) {
6372 return min(self, new ClosureComparator<T>(closure));
6373 }
6374 boolean first = true;
6375 T answer = null;
6376 Object answerValue = null;
6377 for (T item : self) {
6378 Object value = closure.call(item);
6379 if (first) {
6380 first = false;
6381 answer = item;
6382 answerValue = value;
6383 } else if (ScriptBytecodeAdapter.compareLessThan(value, answerValue)) {
6384 answer = item;
6385 answerValue = value;
6386 }
6387 }
6388 return answer;
6389 }
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425 public static <K, V> Map.Entry<K, V> min(Map<K, V> self, @ClosureParams(value=FromString.class, options={"Map.Entry<K,V>", "Map.Entry<K,V>,Map.Entry<K,V>"}) Closure closure) {
6426 return min((Iterable<Map.Entry<K, V>>)self.entrySet(), closure);
6427 }
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463 public static <K, V> Map.Entry<K, V> max(Map<K, V> self, @ClosureParams(value=FromString.class, options={"Map.Entry<K,V>", "Map.Entry<K,V>,Map.Entry<K,V>"}) Closure closure) {
6464 return max((Iterable<Map.Entry<K, V>>)self.entrySet(), closure);
6465 }
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488 public static <T> T min(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
6489 return min((Iterable<T>)toList(self), closure);
6490 }
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511 public static <T> T min(T[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
6512 return min((Iterable<T>)toList(self), closure);
6513 }
6514
6515
6516
6517
6518
6519
6520 @Deprecated
6521 public static <T> T max(Collection<T> self) {
6522 return GroovyCollections.max((Iterable<T>)self);
6523 }
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536 public static <T> T max(Iterable<T> self) {
6537 return GroovyCollections.max(self);
6538 }
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549 public static <T> T max(Iterator<T> self) {
6550 return max((Iterable<T>)toList(self));
6551 }
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561 public static <T> T max(T[] self) {
6562 return max((Iterable<T>)toList(self));
6563 }
6564
6565
6566
6567
6568
6569
6570 @Deprecated
6571 public static <T> T max(Collection<T> self, Closure closure) {
6572 return max((Iterable<T>) self, closure);
6573 }
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602 public static <T> T max(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
6603 int params = closure.getMaximumNumberOfParameters();
6604 if (params != 1) {
6605 return max(self, new ClosureComparator<T>(closure));
6606 }
6607 boolean first = true;
6608 T answer = null;
6609 Object answerValue = null;
6610 for (T item : self) {
6611 Object value = closure.call(item);
6612 if (first) {
6613 first = false;
6614 answer = item;
6615 answerValue = value;
6616 } else if (ScriptBytecodeAdapter.compareLessThan(answerValue, value)) {
6617 answer = item;
6618 answerValue = value;
6619 }
6620 }
6621 return answer;
6622 }
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644 public static <T> T max(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
6645 return max((Iterable<T>)toList(self), closure);
6646 }
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667 public static <T> T max(T[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
6668 return max((Iterable<T>)toList(self), closure);
6669 }
6670
6671
6672
6673
6674
6675
6676 @Deprecated
6677 public static <T> T max(Collection<T> self, Comparator<T> comparator) {
6678 return max((Iterable<T>)self, comparator);
6679 }
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692 public static <T> T max(Iterable<T> self, Comparator<T> comparator) {
6693 T answer = null;
6694 boolean first = true;
6695 for (T value : self) {
6696 if (first) {
6697 first = false;
6698 answer = value;
6699 } else if (comparator.compare(value, answer) > 0) {
6700 answer = value;
6701 }
6702 }
6703 return answer;
6704 }
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714 public static <T> T max(Iterator<T> self, Comparator<T> comparator) {
6715 return max((Iterable<T>)toList(self), comparator);
6716 }
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726 public static <T> T max(T[] self, Comparator<T> comparator) {
6727 return max((Iterable<T>)toList(self), comparator);
6728 }
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742 public static IntRange getIndices(Collection self) {
6743 return new IntRange(false, 0, self.size());
6744 }
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759 public static <T> IntRange getIndices(T[] self) {
6760 return new IntRange(false, 0, self.length);
6761 }
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771 public static int size(Iterator self) {
6772 int count = 0;
6773 while (self.hasNext()) {
6774 self.next();
6775 count++;
6776 }
6777 return count;
6778 }
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792 public static int size(Iterable self) {
6793 return size(self.iterator());
6794 }
6795
6796
6797
6798
6799
6800
6801
6802
6803 public static int size(Object[] self) {
6804 return self.length;
6805 }
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818 public static <T> List<T> getAt(List<T> self, Range range) {
6819 RangeInfo info = subListBorders(self.size(), range);
6820
6821 List<T> subList = self.subList(info.from, info.to);
6822 if (info.reverse) {
6823 subList = reverse(subList);
6824 }
6825
6826
6827 List<T> answer = createSimilarList(self, subList.size());
6828 answer.addAll(subList);
6829
6830 return answer;
6831 }
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845 public static <T> List<T> getAt(ListWithDefault<T> self, Collection indices) {
6846 List<T> answer = ListWithDefault.newInstance(new ArrayList<T>(indices.size()), self.isLazyDefaultValues(), self.getInitClosure());
6847 for (Object value : indices) {
6848 if (value instanceof Range || value instanceof Collection) {
6849 answer.addAll((List<T>) InvokerHelper.invokeMethod(self, "getAt", value));
6850 } else {
6851 int idx = normaliseIndex(DefaultTypeTransformation.intUnbox(value), self.size());
6852 answer.add(self.getAt(idx));
6853 }
6854 }
6855 return answer;
6856 }
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868 public static <T> List<T> getAt(ListWithDefault<T> self, Range range) {
6869 RangeInfo info = subListBorders(self.size(), range);
6870
6871
6872
6873 if (self.size() < info.to) {
6874 self.get(info.to - 1);
6875 }
6876
6877 List<T> answer = self.subList(info.from, info.to);
6878 if (info.reverse) {
6879 answer = ListWithDefault.newInstance(reverse(answer), self.isLazyDefaultValues(), self.getInitClosure());
6880 } else {
6881
6882 answer = ListWithDefault.newInstance(new ArrayList<T>(answer), self.isLazyDefaultValues(), self.getInitClosure());
6883 }
6884
6885 return answer;
6886 }
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899 public static <T> List<T> getAt(ListWithDefault<T> self, EmptyRange range) {
6900 return ListWithDefault.newInstance(new ArrayList<T>(), self.isLazyDefaultValues(), self.getInitClosure());
6901 }
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914 public static <T> List<T> getAt(List<T> self, EmptyRange range) {
6915 return createSimilarList(self, 0);
6916 }
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929 public static <T> List<T> getAt(List<T> self, Collection indices) {
6930 List<T> answer = new ArrayList<T>(indices.size());
6931 for (Object value : indices) {
6932 if (value instanceof Range || value instanceof Collection) {
6933 answer.addAll((List<T>)InvokerHelper.invokeMethod(self, "getAt", value));
6934 } else {
6935 int idx = DefaultTypeTransformation.intUnbox(value);
6936 answer.add(getAt(self, idx));
6937 }
6938 }
6939 return answer;
6940 }
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951 public static <T> List<T> getAt(T[] self, Collection indices) {
6952 List<T> answer = new ArrayList<T>(indices.size());
6953 for (Object value : indices) {
6954 if (value instanceof Range) {
6955 answer.addAll(getAt(self, (Range) value));
6956 } else if (value instanceof Collection) {
6957 answer.addAll(getAt(self, (Collection) value));
6958 } else {
6959 int idx = DefaultTypeTransformation.intUnbox(value);
6960 answer.add(getAtImpl(self, idx));
6961 }
6962 }
6963 return answer;
6964 }
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976 public static <K, V> Map<K, V> subMap(Map<K, V> map, Collection<K> keys) {
6977 Map<K, V> answer = new LinkedHashMap<K, V>(keys.size());
6978 for (K key : keys) {
6979 if (map.containsKey(key)) {
6980 answer.put(key, map.get(key));
6981 }
6982 }
6983 return answer;
6984 }
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002 public static <K, V> Map<K, V> subMap(Map<K, V> map, K[] keys) {
7003 Map<K, V> answer = new LinkedHashMap<K, V>(keys.length);
7004 for (K key : keys) {
7005 if (map.containsKey(key)) {
7006 answer.put(key, map.get(key));
7007 }
7008 }
7009 return answer;
7010 }
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028 public static <K, V> V get(Map<K, V> map, K key, V defaultValue) {
7029 if (!map.containsKey(key)) {
7030 map.put(key, defaultValue);
7031 }
7032 return map.get(key);
7033 }
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044 public static <T> List<T> getAt(T[] array, Range range) {
7045 List<T> list = Arrays.asList(array);
7046 return getAt(list, range);
7047 }
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057 public static <T> List<T> getAt(T[] array, IntRange range) {
7058 List<T> list = Arrays.asList(array);
7059 return getAt(list, range);
7060 }
7061
7062
7063
7064
7065
7066
7067
7068
7069 public static <T> List<T> getAt(T[] array, EmptyRange range) {
7070 return new ArrayList<T>();
7071 }
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081 public static <T> List<T> getAt(T[] array, ObjectRange range) {
7082 List<T> list = Arrays.asList(array);
7083 return getAt(list, range);
7084 }
7085
7086 private static <T> T getAtImpl(T[] array, int idx) {
7087 return array[normaliseIndex(idx, array.length)];
7088 }
7089
7090
7091
7092
7093
7094
7095
7096
7097 public static <T> List<T> toList(T[] array) {
7098 return new ArrayList<T>(Arrays.asList(array));
7099 }
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111 public static <T> T getAt(List<T> self, int idx) {
7112 int size = self.size();
7113 int i = normaliseIndex(idx, size);
7114 if (i < size) {
7115 return self.get(i);
7116 } else {
7117 return null;
7118 }
7119 }
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147 public static <T> T getAt(Iterator<T> self, int idx) {
7148 if (idx < 0) {
7149
7150
7151 List<T> list = toList(self);
7152 int adjustedIndex = idx + list.size();
7153 if (adjustedIndex < 0 || adjustedIndex >= list.size()) return null;
7154 return list.get(adjustedIndex);
7155 }
7156
7157 int count = 0;
7158 while (self.hasNext()) {
7159 if (count == idx) {
7160 return self.next();
7161 } else {
7162 count++;
7163 self.next();
7164 }
7165 }
7166
7167 return null;
7168 }
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190 public static <T> T getAt(Iterable<T> self, int idx) {
7191 return getAt(self.iterator(), idx);
7192 }
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205 public static <T> void putAt(List<T> self, int idx, T value) {
7206 int size = self.size();
7207 idx = normaliseIndex(idx, size);
7208 if (idx < size) {
7209 self.set(idx, value);
7210 } else {
7211 while (size < idx) {
7212 self.add(size++, null);
7213 }
7214 self.add(idx, value);
7215 }
7216 }
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229 public static void putAt(List self, EmptyRange range, Object value) {
7230 RangeInfo info = subListBorders(self.size(), range);
7231 List sublist = self.subList(info.from, info.to);
7232 sublist.clear();
7233 if (value instanceof Collection) {
7234 Collection col = (Collection) value;
7235 if (col.isEmpty()) return;
7236 sublist.addAll(col);
7237 } else {
7238 sublist.add(value);
7239 }
7240 }
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254 public static void putAt(List self, EmptyRange range, Collection value) {
7255 putAt(self, range, (Object)value);
7256 }
7257
7258 private static <T> List<T> resizeListWithRangeAndGetSublist(List<T> self, IntRange range) {
7259 RangeInfo info = subListBorders(self.size(), range);
7260 int size = self.size();
7261 if (info.to >= size) {
7262 while (size < info.to) {
7263 self.add(size++, null);
7264 }
7265 }
7266 List<T> sublist = self.subList(info.from, info.to);
7267 sublist.clear();
7268 return sublist;
7269 }
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286 public static void putAt(List self, IntRange range, Collection col) {
7287 List sublist = resizeListWithRangeAndGetSublist(self, range);
7288 if (col.isEmpty()) return;
7289 sublist.addAll(col);
7290 }
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307 public static void putAt(List self, IntRange range, Object value) {
7308 List sublist = resizeListWithRangeAndGetSublist(self, range);
7309 sublist.add(value);
7310 }
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323 public static void putAt(List self, List splice, List values) {
7324 if (splice.isEmpty()) {
7325 if ( ! values.isEmpty() )
7326 throw new IllegalArgumentException("Trying to replace 0 elements with "+values.size()+" elements");
7327 return;
7328 }
7329 Object first = splice.iterator().next();
7330 if (first instanceof Integer) {
7331 if (values.size() != splice.size())
7332 throw new IllegalArgumentException("Trying to replace "+splice.size()+" elements with "+values.size()+" elements");
7333 Iterator<?> valuesIter = values.iterator();
7334 for (Object index : splice) {
7335 putAt(self, (Integer) index, valuesIter.next());
7336 }
7337 } else {
7338 throw new IllegalArgumentException("Can only index a List with another List of Integers, not a List of "+first.getClass().getName());
7339 }
7340 }
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353 public static void putAt(List self, List splice, Object value) {
7354 if (splice.isEmpty()) {
7355 return;
7356 }
7357 Object first = splice.get(0);
7358 if (first instanceof Integer) {
7359 for (Object index : splice) {
7360 self.set((Integer) index, value);
7361 }
7362 } else {
7363 throw new IllegalArgumentException("Can only index a List with another List of Integers, not a List of "+first.getClass().getName());
7364 }
7365 }
7366
7367
7368 @Deprecated
7369 protected static List getSubList(List self, List splice) {
7370 int left ;
7371 int right = 0;
7372 boolean emptyRange = false;
7373 if (splice.size() == 2) {
7374 left = DefaultTypeTransformation.intUnbox(splice.get(0));
7375 right = DefaultTypeTransformation.intUnbox(splice.get(1));
7376 } else if (splice instanceof IntRange) {
7377 IntRange range = (IntRange) splice;
7378 left = range.getFrom();
7379 right = range.getTo();
7380 } else if (splice instanceof EmptyRange) {
7381 RangeInfo info = subListBorders(self.size(), (EmptyRange) splice);
7382 left = info.from;
7383 emptyRange = true;
7384 } else {
7385 throw new IllegalArgumentException("You must specify a list of 2 indexes to create a sub-list");
7386 }
7387 int size = self.size();
7388 left = normaliseIndex(left, size);
7389 right = normaliseIndex(right, size);
7390 List sublist ;
7391 if (!emptyRange) {
7392 sublist = self.subList(left, right + 1);
7393 } else {
7394 sublist = self.subList(left, left);
7395 }
7396 return sublist;
7397 }
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409 public static <K,V> V getAt(Map<K,V> self, K key) {
7410 return self.get(key);
7411 }
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433 public static <K, V> Map<K, V> plus(Map<K, V> left, Map<K, V> right) {
7434 Map<K, V> map = cloneSimilarMap(left);
7435 map.putAll(right);
7436 return map;
7437 }
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448 public static <K,V> V putAt(Map<K,V> self, K key, V value) {
7449 self.put(key, value);
7450 return value;
7451 }
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464 public static List getAt(Collection coll, String property) {
7465 List<Object> answer = new ArrayList<Object>(coll.size());
7466 return getAtIterable(coll, property, answer);
7467 }
7468
7469 private static List getAtIterable(Iterable coll, String property, List<Object> answer) {
7470 for (Object item : coll) {
7471 if (item == null) continue;
7472 Object value;
7473 try {
7474 value = InvokerHelper.getProperty(item, property);
7475 } catch (MissingPropertyExceptionNoStack mpe) {
7476 String causeString = new MissingPropertyException(mpe.getProperty(), mpe.getType()).toString();
7477 throw new MissingPropertyException("Exception evaluating property '" + property +
7478 "' for " + coll.getClass().getName() + ", Reason: " + causeString);
7479 }
7480 answer.add(value);
7481 }
7482 return answer;
7483 }
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493 public static <K,V> Map<K,V> asImmutable(Map<? extends K, ? extends V> self) {
7494 return Collections.unmodifiableMap(self);
7495 }
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505 public static <K,V> SortedMap<K,V> asImmutable(SortedMap<K, ? extends V> self) {
7506 return Collections.unmodifiableSortedMap(self);
7507 }
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517 public static <T> List<T> asImmutable(List<? extends T> self) {
7518 return Collections.unmodifiableList(self);
7519 }
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529 public static <T> Set<T> asImmutable(Set<? extends T> self) {
7530 return Collections.unmodifiableSet(self);
7531 }
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541 public static <T> SortedSet<T> asImmutable(SortedSet<T> self) {
7542 return Collections.unmodifiableSortedSet(self);
7543 }
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562 public static <T> Collection<T> asImmutable(Collection<? extends T> self) {
7563 return Collections.unmodifiableCollection(self);
7564 }
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574 public static <K,V> Map<K,V> asSynchronized(Map<K,V> self) {
7575 return Collections.synchronizedMap(self);
7576 }
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586 public static <K,V> SortedMap<K,V> asSynchronized(SortedMap<K,V> self) {
7587 return Collections.synchronizedSortedMap(self);
7588 }
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598 public static <T> Collection<T> asSynchronized(Collection<T> self) {
7599 return Collections.synchronizedCollection(self);
7600 }
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610 public static <T> List<T> asSynchronized(List<T> self) {
7611 return Collections.synchronizedList(self);
7612 }
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622 public static <T> Set<T> asSynchronized(Set<T> self) {
7623 return Collections.synchronizedSet(self);
7624 }
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634 public static <T> SortedSet<T> asSynchronized(SortedSet<T> self) {
7635 return Collections.synchronizedSortedSet(self);
7636 }
7637
7638
7639
7640
7641
7642
7643
7644 public static SpreadMap spread(Map self) {
7645 return toSpreadMap(self);
7646 }
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669 public static SpreadMap toSpreadMap(Map self) {
7670 if (self == null)
7671 throw new GroovyRuntimeException("Fail to convert Map to SpreadMap, because it is null.");
7672 else
7673 return new SpreadMap(self);
7674 }
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685 public static SpreadMap toSpreadMap(Object[] self) {
7686 if (self == null)
7687 throw new GroovyRuntimeException("Fail to convert Object[] to SpreadMap, because it is null.");
7688 else if (self.length % 2 != 0)
7689 throw new GroovyRuntimeException("Fail to convert Object[] to SpreadMap, because it's size is not even.");
7690 else
7691 return new SpreadMap(self);
7692 }
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703 public static SpreadMap toSpreadMap(List self) {
7704 if (self == null)
7705 throw new GroovyRuntimeException("Fail to convert List to SpreadMap, because it is null.");
7706 else if (self.size() % 2 != 0)
7707 throw new GroovyRuntimeException("Fail to convert List to SpreadMap, because it's size is not even.");
7708 else
7709 return new SpreadMap(self);
7710 }
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721 public static SpreadMap toSpreadMap(Iterable self) {
7722 if (self == null)
7723 throw new GroovyRuntimeException("Fail to convert Iterable to SpreadMap, because it is null.");
7724 else
7725 return toSpreadMap(asList(self));
7726 }
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749 public static <K, V> Map<K, V> withDefault(Map<K, V> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure init) {
7750 return MapWithDefault.newInstance(self, init);
7751 }
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764 public static <T> List<T> withDefault(List<T> self, Closure init) {
7765 return withLazyDefault(self, init);
7766 }
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810 public static <T> List<T> withLazyDefault(List<T> self, Closure init) {
7811 return ListWithDefault.newInstance(self, true, init);
7812 }
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850 public static <T> List<T> withEagerDefault(List<T> self, Closure init) {
7851 return ListWithDefault.newInstance(self, false, init);
7852 }
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868 public static <E> List<Tuple2<E, Integer>> withIndex(Iterable<E> self) {
7869 return withIndex(self, 0);
7870 }
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886 public static <E> Map<Integer, E> indexed(Iterable<E> self) {
7887 return indexed(self, 0);
7888 }
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905 public static <E> List<Tuple2<E, Integer>> withIndex(Iterable<E> self, int offset) {
7906 return toList(withIndex(self.iterator(), offset));
7907 }
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924 public static <E> Map<Integer, E> indexed(Iterable<E> self, int offset) {
7925 LinkedHashMap<Integer, E> result = new LinkedHashMap<Integer, E>();
7926 Iterator<Tuple2<Integer, E>> indexed = indexed(self.iterator(), offset);
7927 while (indexed.hasNext()) {
7928 Tuple2<Integer, E> next = indexed.next();
7929 result.put(next.getFirst(), next.getSecond());
7930 }
7931 return result;
7932 }
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948 public static <E> Iterator<Tuple2<E, Integer>> withIndex(Iterator<E> self) {
7949 return withIndex(self, 0);
7950 }
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966 public static <E> Iterator<Tuple2<Integer, E>> indexed(Iterator<E> self) {
7967 return indexed(self, 0);
7968 }
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985 public static <E> Iterator<Tuple2<E, Integer>> withIndex(Iterator<E> self, int offset) {
7986 return new ZipPostIterator<E>(self, offset);
7987 }
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004 public static <E> Iterator<Tuple2<Integer, E>> indexed(Iterator<E> self, int offset) {
8005 return new ZipPreIterator<E>(self, offset);
8006 }
8007
8008 private static final class ZipPostIterator<E> implements Iterator<Tuple2<E, Integer>> {
8009 private final Iterator<E> delegate;
8010 private int index;
8011
8012 private ZipPostIterator(Iterator<E> delegate, int offset) {
8013 this.delegate = delegate;
8014 this.index = offset;
8015 }
8016
8017 public boolean hasNext() {
8018 return delegate.hasNext();
8019 }
8020
8021 public Tuple2<E, Integer> next() {
8022 if (!hasNext()) throw new NoSuchElementException();
8023 return new Tuple2<E, Integer>(delegate.next(), index++);
8024 }
8025
8026 public void remove() {
8027 delegate.remove();
8028 }
8029 }
8030
8031 private static final class ZipPreIterator<E> implements Iterator<Tuple2<Integer, E>> {
8032 private final Iterator<E> delegate;
8033 private int index;
8034
8035 private ZipPreIterator(Iterator<E> delegate, int offset) {
8036 this.delegate = delegate;
8037 this.index = offset;
8038 }
8039
8040 public boolean hasNext() {
8041 return delegate.hasNext();
8042 }
8043
8044 public Tuple2<Integer, E> next() {
8045 if (!hasNext()) throw new NoSuchElementException();
8046 return new Tuple2<Integer, E>(index++, delegate.next());
8047 }
8048
8049 public void remove() {
8050 delegate.remove();
8051 }
8052 }
8053
8054
8055
8056
8057
8058
8059 @Deprecated
8060 public static <T> List<T> sort(Collection<T> self) {
8061 return sort((Iterable<T>) self, true);
8062 }
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077 public static <T> List<T> sort(Iterable<T> self) {
8078 return sort(self, true);
8079 }
8080
8081
8082
8083
8084
8085
8086 @Deprecated
8087 public static <T> List<T> sort(Collection<T> self, boolean mutate) {
8088 return sort((Iterable<T>) self, mutate);
8089 }
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110 public static <T> List<T> sort(Iterable<T> self, boolean mutate) {
8111 List<T> answer = mutate ? asList(self) : toList(self);
8112 Collections.sort(answer, new NumberAwareComparator<T>());
8113 return answer;
8114 }
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128 public static <K, V> Map<K, V> sort(Map<K, V> self, @ClosureParams(value=FromString.class, options={"Map.Entry<K,V>","Map.Entry<K,V>,Map.Entry<K,V>"}) Closure closure) {
8129 Map<K, V> result = new LinkedHashMap<K, V>();
8130 List<Map.Entry<K, V>> entries = asList((Iterable<Map.Entry<K, V>>) self.entrySet());
8131 sort((Iterable<Map.Entry<K, V>>) entries, closure);
8132 for (Map.Entry<K, V> entry : entries) {
8133 result.put(entry.getKey(), entry.getValue());
8134 }
8135 return result;
8136 }
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150 public static <K, V> Map<K, V> sort(Map<K, V> self, Comparator<K> comparator) {
8151 Map<K, V> result = new TreeMap<K, V>(comparator);
8152 result.putAll(self);
8153 return result;
8154 }
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168 public static <K, V> Map<K, V> sort(Map<K, V> self) {
8169 return new TreeMap<K, V>(self);
8170 }
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180 public static <T> T[] sort(T[] self) {
8181 Arrays.sort(self, new NumberAwareComparator<T>());
8182 return self;
8183 }
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204 public static <T> T[] sort(T[] self, boolean mutate) {
8205 T[] answer = mutate ? self : self.clone();
8206 Arrays.sort(answer, new NumberAwareComparator<T>());
8207 return answer;
8208 }
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220 public static <T> Iterator<T> sort(Iterator<T> self) {
8221 return sort((Iterable<T>) toList(self)).listIterator();
8222 }
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234 public static <T> Iterator<T> sort(Iterator<T> self, Comparator<T> comparator) {
8235 return sort((Iterable<T>) toList(self), true, comparator).listIterator();
8236 }
8237
8238
8239
8240
8241
8242
8243 @Deprecated
8244 public static <T> List<T> sort(Collection<T> self, Comparator<T> comparator) {
8245 return sort((Iterable<T>) self, true, comparator);
8246 }
8247
8248
8249
8250
8251
8252
8253 @Deprecated
8254 public static <T> List<T> sort(Collection<T> self, boolean mutate, Comparator<T> comparator) {
8255 return sort((Iterable<T>) self, mutate, comparator);
8256 }
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278 public static <T> List<T> sort(Iterable<T> self, boolean mutate, Comparator<T> comparator) {
8279 List<T> list = mutate ? asList(self) : toList(self);
8280 Collections.sort(list, comparator);
8281 return list;
8282 }
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292 public static <T> T[] sort(T[] self, Comparator<T> comparator) {
8293 return sort(self, true, comparator);
8294 }
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315 public static <T> T[] sort(T[] self, boolean mutate, Comparator<T> comparator) {
8316 T[] answer = mutate ? self : self.clone();
8317 Arrays.sort(answer, comparator);
8318 return answer;
8319 }
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337 public static <T> Iterator<T> sort(Iterator<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8338 return sort((Iterable<T>) toList(self), closure).listIterator();
8339 }
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356 @SuppressWarnings("unchecked")
8357 public static <T> T[] sort(T[] self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8358 return sort(self, false, closure);
8359 }
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386 @SuppressWarnings("unchecked")
8387 public static <T> T[] sort(T[] self, boolean mutate, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8388 T[] answer = (T[]) sort((Iterable<T>) toList(self), closure).toArray();
8389 if (mutate) {
8390 System.arraycopy(answer, 0, self, 0, answer.length);
8391 }
8392 return mutate ? self : answer;
8393 }
8394
8395
8396
8397
8398
8399
8400 @Deprecated
8401 public static <T> List<T> sort(Collection<T> self, boolean mutate, Closure closure) {
8402 return sort((Iterable<T>)self, mutate, closure);
8403 }
8404
8405
8406
8407
8408
8409
8410 @Deprecated
8411 public static <T> List<T> sort(Collection<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8412 return sort((Iterable<T>)self, closure);
8413 }
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437 public static <T> List<T> sort(Iterable<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8438 return sort(self, true, closure);
8439 }
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469 public static <T> List<T> sort(Iterable<T> self, boolean mutate, Closure closure) {
8470 List<T> list = mutate ? asList(self) : toList(self);
8471
8472 int params = closure.getMaximumNumberOfParameters();
8473 if (params == 1) {
8474 Collections.sort(list, new OrderBy<T>(closure));
8475 } else {
8476 Collections.sort(list, new ClosureComparator<T>(closure));
8477 }
8478 return list;
8479 }
8480
8481
8482
8483
8484
8485
8486
8487
8488 public static <T> SortedSet<T> sort(SortedSet<T> self) {
8489 return self;
8490 }
8491
8492
8493
8494
8495
8496
8497
8498
8499 public static <K, V> SortedMap<K, V> sort(SortedMap<K, V> self) {
8500 return self;
8501 }
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521 public static <T> List<T> toSorted(Iterable<T> self) {
8522 return toSorted(self, new NumberAwareComparator<T>());
8523 }
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540 public static <T> List<T> toSorted(Iterable<T> self, Comparator<T> comparator) {
8541 List<T> list = toList(self);
8542 Collections.sort(list, comparator);
8543 return list;
8544 }
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567 public static <T> List<T> toSorted(Iterable<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8568 Comparator<T> comparator = (closure.getMaximumNumberOfParameters() == 1) ? new OrderBy<T>(closure) : new ClosureComparator<T>(closure);
8569 return toSorted(self, comparator);
8570 }
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584 public static <T> Iterator<T> toSorted(Iterator<T> self) {
8585 return toSorted(self, new NumberAwareComparator<T>());
8586 }
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598 public static <T> Iterator<T> toSorted(Iterator<T> self, Comparator<T> comparator) {
8599 return toSorted(toList(self), comparator).listIterator();
8600 }
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619 public static <T> Iterator<T> toSorted(Iterator<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) {
8620 Comparator<T> comparator = (closure.getMaximumNumberOfParameters() == 1) ? new OrderBy<T>(closure) : new ClosureComparator<T>(closure);
8621 return toSorted(self, comparator);
8622 }
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632 public static <T> T[] toSorted(T[] self) {
8633 return toSorted(self, new NumberAwareComparator<T>());
8634 }
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651 public static <T> T[] toSorted(T[] self, Comparator<T> comparator) {
8652 T[] answer = self.clone();
8653 Arrays.sort(answer, comparator);
8654 return answer;
8655 }
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673 public static <T> T[] toSorted(T[] self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure condition) {
8674 Comparator<T> comparator = (condition.getMaximumNumberOfParameters() == 1) ? new OrderBy<T>(condition) : new ClosureComparator<T>(condition);
8675 return toSorted(self, comparator);
8676 }
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692 public static <K, V> Map<K, V> toSorted(Map<K, V> self) {
8693 return toSorted(self, new NumberAwareValueComparator<K, V>());
8694 }
8695
8696 private static class NumberAwareValueComparator<K, V> implements Comparator<Map.Entry<K, V>> {
8697 private Comparator<V> delegate = new NumberAwareComparator<V>();
8698
8699 @Override
8700 public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
8701 return delegate.compare(e1.getValue(), e2.getValue());
8702 }
8703 }
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722 public static <K, V> Map<K, V> toSorted(Map<K, V> self, Comparator<Map.Entry<K, V>> comparator) {
8723 List<Map.Entry<K, V>> sortedEntries = toSorted(self.entrySet(), comparator);
8724 Map<K, V> result = new LinkedHashMap<K, V>();
8725 for (Map.Entry<K, V> entry : sortedEntries) {
8726 result.put(entry.getKey(), entry.getValue());
8727 }
8728 return result;
8729 }
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750 public static <K, V> Map<K, V> toSorted(Map<K, V> self, @ClosureParams(value=FromString.class, options={"Map.Entry<K,V>","Map.Entry<K,V>,Map.Entry<K,V>"}) Closure condition) {
8751 Comparator<Map.Entry<K,V>> comparator = (condition.getMaximumNumberOfParameters() == 1) ? new OrderBy<Map.Entry<K,V>>(condition) : new ClosureComparator<Map.Entry<K,V>>(condition);
8752 return toSorted(self, comparator);
8753 }
8754
8755
8756
8757
8758
8759
8760
8761
8762 public static <T> Set<T> toSorted(SortedSet<T> self) {
8763 return new LinkedHashSet<T>(self);
8764 }
8765
8766
8767
8768
8769
8770
8771
8772
8773 public static <K, V> Map<K, V> toSorted(SortedMap<K, V> self) {
8774 return new LinkedHashMap<K, V>(self);
8775 }
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789 public static <T> T pop(List<T> self) {
8790 if (self.isEmpty()) {
8791 throw new NoSuchElementException("Cannot pop() an empty List");
8792 }
8793 return self.remove(self.size() - 1);
8794 }
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804 public static <K, V> Map<K, V> putAll(Map<K, V> self, Collection<Map.Entry<K, V>> entries) {
8805 for (Map.Entry<K, V> entry : entries) {
8806 self.put(entry.getKey(), entry.getValue());
8807 }
8808 return self;
8809 }
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824 public static <K, V> Map<K, V> plus(Map<K, V> self, Collection<Map.Entry<K, V>> entries) {
8825 Map<K, V> map = cloneSimilarMap(self);
8826 putAll(map, entries);
8827 return map;
8828 }
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843 public static <T> boolean push(List<T> self, T value) {
8844 return self.add(value);
8845 }
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861 public static <T> T last(List<T> self) {
8862 if (self.isEmpty()) {
8863 throw new NoSuchElementException("Cannot access last() element from an empty List");
8864 }
8865 return self.get(self.size() - 1);
8866 }
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885 public static <T> T last(Iterable<T> self) {
8886 Iterator<T> iterator = self.iterator();
8887 if (!iterator.hasNext()) {
8888 throw new NoSuchElementException("Cannot access last() element from an empty Iterable");
8889 }
8890 T result = null;
8891 while (iterator.hasNext()) {
8892 result = iterator.next();
8893 }
8894 return result;
8895 }
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909 public static <T> T last(T[] self) {
8910 if (self.length == 0) {
8911 throw new NoSuchElementException("Cannot access last() element from an empty Array");
8912 }
8913 return self[self.length - 1];
8914 }
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930 public static <T> T first(List<T> self) {
8931 if (self.isEmpty()) {
8932 throw new NoSuchElementException("Cannot access first() element from an empty List");
8933 }
8934 return self.get(0);
8935 }
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954 public static <T> T first(Iterable<T> self) {
8955 Iterator<T> iterator = self.iterator();
8956 if (!iterator.hasNext()) {
8957 throw new NoSuchElementException("Cannot access first() element from an empty Iterable");
8958 }
8959 return iterator.next();
8960 }
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974 public static <T> T first(T[] self) {
8975 if (self.length == 0) {
8976 throw new NoSuchElementException("Cannot access first() element from an empty array");
8977 }
8978 return self[0];
8979 }
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998 public static <T> T head(Iterable<T> self) {
8999 return first(self);
9000 }
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013 public static <T> T head(List<T> self) {
9014 return first(self);
9015 }
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027 public static <T> T head(T[] self) {
9028 return first(self);
9029 }
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044 public static <T> List<T> tail(List<T> self) {
9045 return (List<T>) tail((Iterable<T>)self);
9046 }
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061 public static <T> SortedSet<T> tail(SortedSet<T> self) {
9062 return (SortedSet<T>) tail((Iterable<T>) self);
9063 }
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078 public static <T> Collection<T> tail(Iterable<T> self) {
9079 if (!self.iterator().hasNext()) {
9080 throw new NoSuchElementException("Cannot access tail() for an empty iterable");
9081 }
9082 Collection<T> result = createSimilarCollection(self);
9083 addAll(result, tail(self.iterator()));
9084 return result;
9085 }
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102 @SuppressWarnings("unchecked")
9103 public static <T> T[] tail(T[] self) {
9104 if (self.length == 0) {
9105 throw new NoSuchElementException("Cannot access tail() for an empty array");
9106 }
9107 T[] result = createSimilarArray(self, self.length - 1);
9108 System.arraycopy(self, 1, result, 0, self.length - 1);
9109 return result;
9110 }
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120 public static <T> Iterator<T> tail(Iterator<T> self) {
9121 if (!self.hasNext()) {
9122 throw new NoSuchElementException("Cannot access tail() for an empty Iterator");
9123 }
9124 self.next();
9125 return self;
9126 }
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141 public static <T> Collection<T> init(Iterable<T> self) {
9142 if (!self.iterator().hasNext()) {
9143 throw new NoSuchElementException("Cannot access init() for an empty Iterable");
9144 }
9145 Collection<T> result;
9146 if (self instanceof Collection) {
9147 Collection<T> selfCol = (Collection<T>) self;
9148 result = createSimilarCollection(selfCol, selfCol.size() - 1);
9149 } else {
9150 result = new ArrayList<T>();
9151 }
9152 addAll(result, init(self.iterator()));
9153 return result;
9154 }
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169 public static <T> List<T> init(List<T> self) {
9170 return (List<T>) init((Iterable<T>) self);
9171 }
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186 public static <T> SortedSet<T> init(SortedSet<T> self) {
9187 return (SortedSet<T>) init((Iterable<T>) self);
9188 }
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203 public static <T> Iterator<T> init(Iterator<T> self) {
9204 if (!self.hasNext()) {
9205 throw new NoSuchElementException("Cannot access init() for an empty Iterator");
9206 }
9207 return new InitIterator<T>(self);
9208 }
9209
9210 private static final class InitIterator<E> implements Iterator<E> {
9211 private final Iterator<E> delegate;
9212 private boolean exhausted;
9213 private E next;
9214
9215 private InitIterator(Iterator<E> delegate) {
9216 this.delegate = delegate;
9217 advance();
9218 }
9219
9220 public boolean hasNext() {
9221 return !exhausted;
9222 }
9223
9224 public E next() {
9225 if (exhausted) throw new NoSuchElementException();
9226 E result = next;
9227 advance();
9228 return result;
9229 }
9230
9231 public void remove() {
9232 if (exhausted) throw new NoSuchElementException();
9233 advance();
9234 }
9235
9236 private void advance() {
9237 next = delegate.next();
9238 exhausted = !delegate.hasNext();
9239 }
9240 }
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256 public static <T> T[] init(T[] self) {
9257 if (self.length == 0) {
9258 throw new NoSuchElementException("Cannot access init() for an empty Object array");
9259 }
9260 T[] result = createSimilarArray(self, self.length - 1);
9261 System.arraycopy(self, 0, result, 0, self.length - 1);
9262 return result;
9263 }
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280 public static <T> List<T> take(List<T> self, int num) {
9281 return (List<T>) take((Iterable<T>)self, num);
9282 }
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299 public static <T> SortedSet<T> take(SortedSet<T> self, int num) {
9300 return (SortedSet<T>) take((Iterable<T>) self, num);
9301 }
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318 public static <T> T[] take(T[] self, int num) {
9319 if (self.length == 0 || num <= 0) {
9320 return createSimilarArray(self, 0);
9321 }
9322
9323 if (self.length <= num) {
9324 T[] ret = createSimilarArray(self, self.length);
9325 System.arraycopy(self, 0, ret, 0, self.length);
9326 return ret;
9327 }
9328
9329 T[] ret = createSimilarArray(self, num);
9330 System.arraycopy(self, 0, ret, 0, num);
9331 return ret;
9332 }
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358 public static <T> Collection<T> take(Iterable<T> self, int num) {
9359 Collection<T> result = self instanceof Collection ? createSimilarCollection((Collection<T>) self, num < 0 ? 0 : num) : new ArrayList<T>();
9360 addAll(result, take(self.iterator(), num));
9361 return result;
9362 }
9363
9364
9365
9366
9367
9368
9369
9370
9371 public static <T> boolean addAll(Collection<T> self, Iterator<T> items) {
9372 boolean changed = false;
9373 while (items.hasNext()) {
9374 T next = items.next();
9375 if (self.add(next)) changed = true;
9376 }
9377 return changed;
9378 }
9379
9380
9381
9382
9383
9384
9385
9386
9387 public static <T> boolean addAll(Collection<T> self, Iterable<T> items) {
9388 boolean changed = false;
9389 for (T next : items) {
9390 if (self.add(next)) changed = true;
9391 }
9392 return changed;
9393 }
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412 public static <K, V> Map<K, V> take(Map<K, V> self, int num) {
9413 if (self.isEmpty() || num <= 0) {
9414 return createSimilarMap(self);
9415 }
9416 Map<K, V> ret = createSimilarMap(self);
9417 for (K key : self.keySet()) {
9418 ret.put(key, self.get(key));
9419 if (--num <= 0) {
9420 break;
9421 }
9422 }
9423 return ret;
9424 }
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445 public static <T> Iterator<T> take(Iterator<T> self, int num) {
9446 return new TakeIterator<T>(self, num);
9447 }
9448
9449 private static final class TakeIterator<E> implements Iterator<E> {
9450 private final Iterator<E> delegate;
9451 private Integer num;
9452
9453 private TakeIterator(Iterator<E> delegate, Integer num) {
9454 this.delegate = delegate;
9455 this.num = num;
9456 }
9457
9458 public boolean hasNext() {
9459 return delegate.hasNext() && num > 0;
9460 }
9461
9462 public E next() {
9463 if (num <= 0) throw new NoSuchElementException();
9464 num--;
9465 return delegate.next();
9466 }
9467
9468 public void remove() {
9469 delegate.remove();
9470 }
9471 }
9472
9473 @Deprecated
9474 public static CharSequence take(CharSequence self, int num) {
9475 return StringGroovyMethods.take(self, num);
9476 }
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493 public static <T> T[] takeRight(T[] self, int num) {
9494 if (self.length == 0 || num <= 0) {
9495 return createSimilarArray(self, 0);
9496 }
9497
9498 if (self.length <= num) {
9499 T[] ret = createSimilarArray(self, self.length);
9500 System.arraycopy(self, 0, ret, 0, self.length);
9501 return ret;
9502 }
9503
9504 T[] ret = createSimilarArray(self, num);
9505 System.arraycopy(self, self.length - num, ret, 0, num);
9506 return ret;
9507 }
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533 public static <T> Collection<T> takeRight(Iterable<T> self, int num) {
9534 if (!self.iterator().hasNext() || num <= 0) {
9535 return self instanceof Collection ? createSimilarCollection((Collection<T>) self, 0) : new ArrayList<T>();
9536 }
9537 Collection<T> selfCol = self instanceof Collection ? (Collection<T>) self : toList(self);
9538 if (selfCol.size() <= num) {
9539 Collection<T> ret = createSimilarCollection(selfCol, selfCol.size());
9540 ret.addAll(selfCol);
9541 return ret;
9542 }
9543 Collection<T> ret = createSimilarCollection(selfCol, num);
9544 ret.addAll(asList((Iterable<T>) selfCol).subList(selfCol.size() - num, selfCol.size()));
9545 return ret;
9546 }
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563 public static <T> List<T> takeRight(List<T> self, int num) {
9564 return (List<T>) takeRight((Iterable<T>) self, num);
9565 }
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582 public static <T> SortedSet<T> takeRight(SortedSet<T> self, int num) {
9583 return (SortedSet<T>) takeRight((Iterable<T>) self, num);
9584 }
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601 public static <T> SortedSet<T> drop(SortedSet<T> self, int num) {
9602 return (SortedSet<T>) drop((Iterable<T>) self, num);
9603 }
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620 public static <T> List<T> drop(List<T> self, int num) {
9621 return (List<T>) drop((Iterable<T>) self, num);
9622 }
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648 public static <T> Collection<T> drop(Iterable<T> self, int num) {
9649 Collection<T> result = createSimilarCollection(self);
9650 addAll(result, drop(self.iterator(), num));
9651 return result;
9652 }
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671 public static <T> T[] drop(T[] self, int num) {
9672 if (self.length <= num) {
9673 return createSimilarArray(self, 0);
9674 }
9675 if (num <= 0) {
9676 T[] ret = createSimilarArray(self, self.length);
9677 System.arraycopy(self, 0, ret, 0, self.length);
9678 return ret;
9679 }
9680
9681 T[] ret = createSimilarArray(self, self.length - num);
9682 System.arraycopy(self, num, ret, 0, self.length - num);
9683 return ret;
9684 }
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704 public static <K, V> Map<K, V> drop(Map<K, V> self, int num) {
9705 if (self.size() <= num) {
9706 return createSimilarMap(self);
9707 }
9708 if (num == 0) {
9709 return cloneSimilarMap(self);
9710 }
9711 Map<K, V> ret = createSimilarMap(self);
9712 for (K key : self.keySet()) {
9713 if (num-- <= 0) {
9714 ret.put(key, self.get(key));
9715 }
9716 }
9717 return ret;
9718 }
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740 public static <T> Iterator<T> drop(Iterator<T> self, int num) {
9741 while (num-- > 0 && self.hasNext()) {
9742 self.next();
9743 }
9744 return self;
9745 }
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762 public static <T> SortedSet<T> dropRight(SortedSet<T> self, int num) {
9763 return (SortedSet<T>) dropRight((Iterable<T>) self, num);
9764 }
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781 public static <T> List<T> dropRight(List<T> self, int num) {
9782 return (List<T>) dropRight((Iterable<T>) self, num);
9783 }
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809 public static <T> Collection<T> dropRight(Iterable<T> self, int num) {
9810 Collection<T> selfCol = self instanceof Collection ? (Collection<T>) self : toList(self);
9811 if (selfCol.size() <= num) {
9812 return createSimilarCollection(selfCol, 0);
9813 }
9814 if (num <= 0) {
9815 Collection<T> ret = createSimilarCollection(selfCol, selfCol.size());
9816 ret.addAll(selfCol);
9817 return ret;
9818 }
9819 Collection<T> ret = createSimilarCollection(selfCol, selfCol.size() - num);
9820 ret.addAll(asList((Iterable<T>)selfCol).subList(0, selfCol.size() - num));
9821 return ret;
9822 }
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843 public static <T> Iterator<T> dropRight(Iterator<T> self, int num) {
9844 List<T> result = dropRight(toList(self), num);
9845 return result.listIterator();
9846 }
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865 public static <T> T[] dropRight(T[] self, int num) {
9866 if (self.length <= num) {
9867 return createSimilarArray(self, 0);
9868 }
9869 if (num <= 0) {
9870 T[] ret = createSimilarArray(self, self.length);
9871 System.arraycopy(self, 0, ret, 0, self.length);
9872 return ret;
9873 }
9874
9875 T[] ret = createSimilarArray(self, self.length - num);
9876 System.arraycopy(self, 0, ret, 0, self.length - num);
9877 return ret;
9878 }
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899 public static <T> List<T> takeWhile(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
9900 int num = 0;
9901 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
9902 for (T value : self) {
9903 if (bcw.call(value)) {
9904 num += 1;
9905 } else {
9906 break;
9907 }
9908 }
9909 return take(self, num);
9910 }
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931 public static <T> Collection<T> takeWhile(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
9932 Collection<T> result = createSimilarCollection(self);
9933 addAll(result, takeWhile(self.iterator(), condition));
9934 return result;
9935 }
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956 public static <T> SortedSet<T> takeWhile(SortedSet<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
9957 return (SortedSet<T>) takeWhile((Iterable<T>) self, condition);
9958 }
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979 public static <K, V> Map<K, V> takeWhile(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> condition) {
9980 if (self.isEmpty()) {
9981 return createSimilarMap(self);
9982 }
9983 Map<K, V> ret = createSimilarMap(self);
9984 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
9985 for (Map.Entry<K, V> entry : self.entrySet()) {
9986 if (!bcw.callForMap(entry)) break;
9987 ret.put(entry.getKey(), entry.getValue());
9988 }
9989 return ret;
9990 }
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009 public static <T> T[] takeWhile(T[] self, @ClosureParams(FirstParam.Component.class) Closure condition) {
10010 int num = 0;
10011 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
10012 while (num < self.length) {
10013 T value = self[num];
10014 if (bcw.call(value)) {
10015 num += 1;
10016 } else {
10017 break;
10018 }
10019 }
10020 return take(self, num);
10021 }
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043 public static <T> Iterator<T> takeWhile(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
10044 return new TakeWhileIterator<T>(self, condition);
10045 }
10046
10047 private static final class TakeWhileIterator<E> implements Iterator<E> {
10048 private final Iterator<E> delegate;
10049 private final BooleanClosureWrapper condition;
10050 private boolean exhausted;
10051 private E next;
10052
10053 private TakeWhileIterator(Iterator<E> delegate, Closure condition) {
10054 this.delegate = delegate;
10055 this.condition = new BooleanClosureWrapper(condition);
10056 advance();
10057 }
10058
10059 public boolean hasNext() {
10060 return !exhausted;
10061 }
10062
10063 public E next() {
10064 if (exhausted) throw new NoSuchElementException();
10065 E result = next;
10066 advance();
10067 return result;
10068 }
10069
10070 public void remove() {
10071 if (exhausted) throw new NoSuchElementException();
10072 delegate.remove();
10073 }
10074
10075 private void advance() {
10076 exhausted = !delegate.hasNext();
10077 if (!exhausted) {
10078 next = delegate.next();
10079 if (!condition.call(next)) {
10080 exhausted = true;
10081 next = null;
10082 }
10083 }
10084 }
10085 }
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106 public static <T> SortedSet<T> dropWhile(SortedSet<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> condition) {
10107 return (SortedSet<T>) dropWhile((Iterable<T>) self, condition);
10108 }
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129 public static <T> List<T> dropWhile(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> condition) {
10130 int num = 0;
10131 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
10132 for (T value : self) {
10133 if (bcw.call(value)) {
10134 num += 1;
10135 } else {
10136 break;
10137 }
10138 }
10139 return drop(self, num);
10140 }
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160 public static <T> Collection<T> dropWhile(Iterable<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> condition) {
10161 Collection<T> selfCol = self instanceof Collection ? (Collection<T>) self : toList(self);
10162 Collection<T> result = createSimilarCollection(selfCol);
10163 addAll(result, dropWhile(self.iterator(), condition));
10164 return result;
10165 }
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187 public static <K, V> Map<K, V> dropWhile(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> condition) {
10188 if (self.isEmpty()) {
10189 return createSimilarMap(self);
10190 }
10191 Map<K, V> ret = createSimilarMap(self);
10192 boolean dropping = true;
10193 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
10194 for (Map.Entry<K, V> entry : self.entrySet()) {
10195 if (dropping && !bcw.callForMap(entry)) dropping = false;
10196 if (!dropping) ret.put(entry.getKey(), entry.getValue());
10197 }
10198 return ret;
10199 }
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220 public static <T> T[] dropWhile(T[] self, @ClosureParams(FirstParam.Component.class) Closure<?> condition) {
10221 int num = 0;
10222 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
10223 while (num < self.length) {
10224 if (bcw.call(self[num])) {
10225 num += 1;
10226 } else {
10227 break;
10228 }
10229 }
10230 return drop(self, num);
10231 }
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251 public static <T> Iterator<T> dropWhile(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<?> condition) {
10252 return new DropWhileIterator<T>(self, condition);
10253 }
10254
10255 private static final class DropWhileIterator<E> implements Iterator<E> {
10256 private final Iterator<E> delegate;
10257 private final Closure condition;
10258 private boolean buffering = false;
10259 private E buffer = null;
10260
10261 private DropWhileIterator(Iterator<E> delegate, Closure condition) {
10262 this.delegate = delegate;
10263 this.condition = condition;
10264 prepare();
10265 }
10266
10267 public boolean hasNext() {
10268 return buffering || delegate.hasNext();
10269 }
10270
10271 public E next() {
10272 if (buffering) {
10273 E result = buffer;
10274 buffering = false;
10275 buffer = null;
10276 return result;
10277 } else {
10278 return delegate.next();
10279 }
10280 }
10281
10282 public void remove() {
10283 if (buffering) {
10284 buffering = false;
10285 buffer = null;
10286 } else {
10287 delegate.remove();
10288 }
10289 }
10290
10291 private void prepare() {
10292 BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
10293 while (delegate.hasNext()) {
10294 E next = delegate.next();
10295 if (!bcw.call(next)) {
10296 buffer = next;
10297 buffering = true;
10298 break;
10299 }
10300 }
10301 }
10302 }
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317 public static <T> Collection<T> asCollection(Iterable<T> self) {
10318 if (self instanceof Collection) {
10319 return (Collection<T>) self;
10320 } else {
10321 return toList(self);
10322 }
10323 }
10324
10325
10326
10327
10328
10329
10330 @Deprecated
10331 public static <T> List<T> asList(Collection<T> self) {
10332 return asList((Iterable<T>)self);
10333 }
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348 public static <T> List<T> asList(Iterable<T> self) {
10349 if (self instanceof List) {
10350 return (List<T>) self;
10351 } else {
10352 return toList(self);
10353 }
10354 }
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364 public static boolean asBoolean(Object object) {
10365 return object != null;
10366 }
10367
10368
10369
10370
10371
10372
10373
10374
10375 public static boolean asBoolean(Boolean bool) {
10376 return bool;
10377 }
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389 public static boolean asBoolean(Collection collection) {
10390 return !collection.isEmpty();
10391 }
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403 public static boolean asBoolean(Map map) {
10404 return !map.isEmpty();
10405 }
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416 public static boolean asBoolean(Iterator iterator) {
10417 return iterator.hasNext();
10418 }
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429 public static boolean asBoolean(Enumeration enumeration) {
10430 return enumeration.hasMoreElements();
10431 }
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442 public static boolean asBoolean(Object[] array) {
10443 return array.length > 0;
10444 }
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455 public static boolean asBoolean(byte[] array) {
10456 return array.length > 0;
10457 }
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468 public static boolean asBoolean(short[] array) {
10469 return array.length > 0;
10470 }
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481 public static boolean asBoolean(int[] array) {
10482 return array.length > 0;
10483 }
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494 public static boolean asBoolean(long[] array) {
10495 return array.length > 0;
10496 }
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507 public static boolean asBoolean(float[] array) {
10508 return array.length > 0;
10509 }
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520 public static boolean asBoolean(double[] array) {
10521 return array.length > 0;
10522 }
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533 public static boolean asBoolean(boolean[] array) {
10534 return array.length > 0;
10535 }
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546 public static boolean asBoolean(char[] array) {
10547 return array.length > 0;
10548 }
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560 public static boolean asBoolean(Character character) {
10561 return character != 0;
10562 }
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573 public static boolean asBoolean(Number number) {
10574 return number.doubleValue() != 0;
10575 }
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591 @SuppressWarnings("unchecked")
10592 public static <T> T asType(Collection col, Class<T> clazz) {
10593 if (col.getClass() == clazz) {
10594 return (T) col;
10595 }
10596 if (clazz == List.class) {
10597 return (T) asList((Iterable) col);
10598 }
10599 if (clazz == Set.class) {
10600 if (col instanceof Set) return (T) col;
10601 return (T) new LinkedHashSet(col);
10602 }
10603 if (clazz == SortedSet.class) {
10604 if (col instanceof SortedSet) return (T) col;
10605 return (T) new TreeSet(col);
10606 }
10607 if (clazz == Queue.class) {
10608 if (col instanceof Queue) return (T) col;
10609 return (T) new LinkedList(col);
10610 }
10611 if (clazz == Stack.class) {
10612 if (col instanceof Stack) return (T) col;
10613 final Stack stack = new Stack();
10614 stack.addAll(col);
10615 return (T) stack;
10616 }
10617
10618 if (clazz!=String[].class && ReflectionCache.isArray(clazz)) {
10619 try {
10620 return (T) asArrayType(col, clazz);
10621 } catch (GroovyCastException e) {
10622
10623 }
10624 }
10625
10626 Object[] args = {col};
10627 try {
10628 return (T) InvokerHelper.invokeConstructorOf(clazz, args);
10629 } catch (Exception e) {
10630
10631 }
10632 if (Collection.class.isAssignableFrom(clazz)) {
10633 try {
10634 Collection result = (Collection) InvokerHelper.invokeConstructorOf(clazz, null);
10635 result.addAll(col);
10636 return (T)result;
10637 } catch (Exception e) {
10638
10639 }
10640 }
10641
10642 return asType((Object) col, clazz);
10643 }
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656 @SuppressWarnings("unchecked")
10657 public static <T> T asType(Object[] ary, Class<T> clazz) {
10658 if (clazz == List.class) {
10659 return (T) new ArrayList(Arrays.asList(ary));
10660 }
10661 if (clazz == Set.class) {
10662 return (T) new HashSet(Arrays.asList(ary));
10663 }
10664 if (clazz == SortedSet.class) {
10665 return (T) new TreeSet(Arrays.asList(ary));
10666 }
10667
10668 return asType((Object) ary, clazz);
10669 }
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681 @SuppressWarnings("unchecked")
10682 public static <T> T asType(Closure cl, Class<T> clazz) {
10683 if (clazz.isInterface() && !(clazz.isInstance(cl))) {
10684 if (Traits.isTrait(clazz)) {
10685 Method samMethod = CachedSAMClass.getSAMMethod(clazz);
10686 if (samMethod!=null) {
10687 Map impl = Collections.singletonMap(samMethod.getName(),cl);
10688 return (T) ProxyGenerator.INSTANCE.instantiateAggregate(impl, Collections.<Class>singletonList(clazz));
10689 }
10690 }
10691 return (T) Proxy.newProxyInstance(
10692 clazz.getClassLoader(),
10693 new Class[]{clazz},
10694 new ConvertedClosure(cl));
10695 }
10696 try {
10697 return asType((Object) cl, clazz);
10698 } catch (GroovyCastException ce) {
10699 try {
10700 return (T) ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(cl, clazz);
10701 } catch (GroovyRuntimeException cause) {
10702 throw new GroovyCastException("Error casting closure to " + clazz.getName() +
10703 ", Reason: " + cause.getMessage());
10704 }
10705 }
10706 }
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718 @SuppressWarnings("unchecked")
10719 public static <T> T asType(Map map, Class<T> clazz) {
10720 if (!(clazz.isInstance(map)) && clazz.isInterface() && !Traits.isTrait(clazz)) {
10721 return (T) Proxy.newProxyInstance(
10722 clazz.getClassLoader(),
10723 new Class[]{clazz},
10724 new ConvertedMap(map));
10725 }
10726 try {
10727 return asType((Object) map, clazz);
10728 } catch (GroovyCastException ce) {
10729 try {
10730 return (T) ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(map, clazz);
10731 } catch (GroovyRuntimeException cause) {
10732 throw new GroovyCastException("Error casting map to " + clazz.getName() +
10733 ", Reason: " + cause.getMessage());
10734 }
10735 }
10736 }
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752 public static <T> List<T> reverse(List<T> self) {
10753 return reverse(self, false);
10754 }
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772 public static <T> List<T> reverse(List<T> self, boolean mutate) {
10773 if (mutate) {
10774 Collections.reverse(self);
10775 return self;
10776 }
10777 int size = self.size();
10778 List<T> answer = new ArrayList<T>(size);
10779 ListIterator<T> iter = self.listIterator(size);
10780 while (iter.hasPrevious()) {
10781 answer.add(iter.previous());
10782 }
10783 return answer;
10784 }
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794 @SuppressWarnings("unchecked")
10795 public static <T> T[] reverse(T[] self) {
10796 return reverse(self, false);
10797 }
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808 @SuppressWarnings("unchecked")
10809 public static <T> T[] reverse(T[] self, boolean mutate) {
10810 if (!mutate) {
10811 return (T[]) toList(new ReverseListIterator<T>(Arrays.asList(self))).toArray();
10812 }
10813 List<T> items = Arrays.asList(self);
10814 Collections.reverse(items);
10815 System.arraycopy(items.toArray(), 0, self, 0, items.size());
10816 return self;
10817 }
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828 public static <T> Iterator<T> reverse(Iterator<T> self) {
10829 return new ReverseListIterator<T>(toList(self));
10830 }
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845 @SuppressWarnings("unchecked")
10846 public static <T> T[] plus(T[] left, T[] right) {
10847 return (T[]) plus(toList(left), toList(right)).toArray();
10848 }
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863 @SuppressWarnings("unchecked")
10864 public static <T> T[] plus(T[] left, T right) {
10865 return (T[]) plus(toList(left), right).toArray();
10866 }
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881 @SuppressWarnings("unchecked")
10882 public static <T> T[] plus(T[] left, Collection<T> right) {
10883 return (T[]) plus(toList(left), right).toArray();
10884 }
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903 @SuppressWarnings("unchecked")
10904 public static <T> T[] plus(T[] left, Iterable<T> right) {
10905 return (T[]) plus(toList(left), toList(right)).toArray();
10906 }
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920 public static <T> Collection<T> plus(Collection<T> left, Collection<T> right) {
10921 final Collection<T> answer = cloneSimilarCollection(left, left.size() + right.size());
10922 answer.addAll(right);
10923 return answer;
10924 }
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938 public static <T> Collection<T> plus(Iterable<T> left, Iterable<T> right) {
10939 return plus(asCollection(left), asCollection(right));
10940 }
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954 public static <T> Collection<T> plus(Collection<T> left, Iterable<T> right) {
10955 return plus(left, asCollection(right));
10956 }
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969 public static <T> List<T> plus(List<T> left, Iterable<T> right) {
10970 return (List<T>) plus((Collection<T>) left, asCollection(right));
10971 }
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984 public static <T> List<T> plus(List<T> left, Collection<T> right) {
10985 return (List<T>) plus((Collection<T>) left, right);
10986 }
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999 public static <T> Set<T> plus(Set<T> left, Iterable<T> right) {
11000 return (Set<T>) plus((Collection<T>) left, asCollection(right));
11001 }
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014 public static <T> Set<T> plus(Set<T> left, Collection<T> right) {
11015 return (Set<T>) plus((Collection<T>) left, right);
11016 }
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029 public static <T> SortedSet<T> plus(SortedSet<T> left, Iterable<T> right) {
11030 return (SortedSet<T>) plus((Collection<T>) left, asCollection(right));
11031 }
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044 public static <T> SortedSet<T> plus(SortedSet<T> left, Collection<T> right) {
11045 return (SortedSet<T>) plus((Collection<T>) left, right);
11046 }
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076 public static <T> List<T> plus(List<T> self, int index, T[] items) {
11077 return plus(self, index, Arrays.asList(items));
11078 }
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105 public static <T> List<T> plus(List<T> self, int index, List<T> additions) {
11106 final List<T> answer = new ArrayList<T>(self);
11107 answer.addAll(index, additions);
11108 return answer;
11109 }
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122 public static <T> List<T> plus(List<T> self, int index, Iterable<T> additions) {
11123 return plus(self, index, toList(additions));
11124 }
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138 public static <T> Collection<T> plus(Collection<T> left, T right) {
11139 final Collection<T> answer = cloneSimilarCollection(left, left.size() + 1);
11140 answer.add(right);
11141 return answer;
11142 }
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156 public static <T> Collection<T> plus(Iterable<T> left, T right) {
11157 return plus(asCollection(left), right);
11158 }
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171 public static <T> List<T> plus(List<T> left, T right) {
11172 return (List<T>) plus((Collection<T>) left, right);
11173 }
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186 public static <T> Set<T> plus(Set<T> left, T right) {
11187 return (Set<T>) plus((Collection<T>) left, right);
11188 }
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201 public static <T> SortedSet<T> plus(SortedSet<T> left, T right) {
11202 return (SortedSet<T>) plus((Collection<T>) left, right);
11203 }
11204
11205
11206
11207
11208
11209
11210 @Deprecated
11211 public static <T> Collection<T> multiply(Collection<T> self, Number factor) {
11212 return multiply((Iterable<T>) self, factor);
11213 }
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229 public static <T> Collection<T> multiply(Iterable<T> self, Number factor) {
11230 Collection<T> selfCol = asCollection(self);
11231 int size = factor.intValue();
11232 Collection<T> answer = createSimilarCollection(selfCol, selfCol.size() * size);
11233 for (int i = 0; i < size; i++) {
11234 answer.addAll(selfCol);
11235 }
11236 return answer;
11237 }
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253 public static <T> List<T> multiply(List<T> self, Number factor) {
11254 return (List<T>) multiply((Iterable<T>) self, factor);
11255 }
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267 public static <T> Collection<T> intersect(Collection<T> left, Collection<T> right) {
11268 if (left.isEmpty() || right.isEmpty())
11269 return createSimilarCollection(left, 0);
11270
11271 if (left.size() < right.size()) {
11272 Collection<T> swaptemp = left;
11273 left = right;
11274 right = swaptemp;
11275 }
11276
11277
11278
11279
11280 Collection<T> result = createSimilarCollection(left, left.size());
11281
11282 Collection<T> pickFrom = new TreeSet<T>(new NumberAwareComparator<T>());
11283 pickFrom.addAll(left);
11284
11285 for (final T t : right) {
11286 if (pickFrom.contains(t))
11287 result.add(t);
11288 }
11289 return result;
11290 }
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302 public static <T> Collection<T> intersect(Iterable<T> left, Iterable<T> right) {
11303 return intersect(asCollection(left), asCollection(right));
11304 }
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316 public static <T> List<T> intersect(List<T> left, Iterable<T> right) {
11317 return (List<T>) intersect((Collection<T>) left, asCollection(right));
11318 }
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330 public static <T> Set<T> intersect(Set<T> left, Iterable<T> right) {
11331 return (Set<T>) intersect((Collection<T>) left, asCollection(right));
11332 }
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344 public static <T> SortedSet<T> intersect(SortedSet<T> left, Iterable<T> right) {
11345 return (SortedSet<T>) intersect((Collection<T>) left, asCollection(right));
11346 }
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359 public static <K,V> Map<K,V> intersect(Map<K,V> left, Map<K,V> right) {
11360 final Map<K,V> ansMap = createSimilarMap(left);
11361 if (right != null && right.size() > 0) {
11362 for (Map.Entry<K, V> e1 : left.entrySet()) {
11363 for (Map.Entry<K, V> e2 : right.entrySet()) {
11364 if (DefaultTypeTransformation.compareEqual(e1, e2)) {
11365 ansMap.put(e1.getKey(), e1.getValue());
11366 }
11367 }
11368 }
11369 }
11370 return ansMap;
11371 }
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384 public static boolean disjoint(Iterable left, Iterable right) {
11385 Collection leftCol = asCollection(left);
11386 Collection rightCol = asCollection(right);
11387
11388 if (leftCol.isEmpty() || rightCol.isEmpty())
11389 return true;
11390
11391 Collection pickFrom = new TreeSet(new NumberAwareComparator());
11392 pickFrom.addAll(rightCol);
11393
11394 for (final Object o : leftCol) {
11395 if (pickFrom.contains(o))
11396 return false;
11397 }
11398 return true;
11399 }
11400
11401
11402
11403
11404
11405
11406 @Deprecated
11407 public static boolean disjoint(Collection left, Collection right) {
11408 return disjoint((Iterable) left, (Iterable) right);
11409 }
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419 public static boolean equals(int[] left, int[] right) {
11420 if (left == null) {
11421 return right == null;
11422 }
11423 if (right == null) {
11424 return false;
11425 }
11426 if (left == right) {
11427 return true;
11428 }
11429 if (left.length != right.length) {
11430 return false;
11431 }
11432 for (int i = 0; i < left.length; i++) {
11433 if (left[i] != right[i]) return false;
11434 }
11435 return true;
11436 }
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448 public static boolean equals(Object[] left, List right) {
11449 return coercedEquals(left, right);
11450 }
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463 public static boolean equals(List left, Object[] right) {
11464 return coercedEquals(right, left);
11465 }
11466
11467 private static boolean coercedEquals(Object[] left, List right) {
11468 if (left == null) {
11469 return right == null;
11470 }
11471 if (right == null) {
11472 return false;
11473 }
11474 if (left.length != right.size()) {
11475 return false;
11476 }
11477 for (int i = left.length - 1; i >= 0; i--) {
11478 final Object o1 = left[i];
11479 final Object o2 = right.get(i);
11480 if (o1 == null) {
11481 if (o2 != null) return false;
11482 } else if (!coercedEquals(o1, o2)) {
11483 return false;
11484 }
11485 }
11486 return true;
11487 }
11488
11489 private static boolean coercedEquals(Object o1, Object o2) {
11490 if (o1 instanceof Comparable) {
11491 if (!(o2 instanceof Comparable && numberAwareCompareTo((Comparable) o1, (Comparable) o2) == 0)) {
11492 return false;
11493 }
11494 }
11495 return DefaultTypeTransformation.compareEqual(o1, o2);
11496 }
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514 public static boolean equals(List left, List right) {
11515 if (left == null) {
11516 return right == null;
11517 }
11518 if (right == null) {
11519 return false;
11520 }
11521 if (left == right) {
11522 return true;
11523 }
11524 if (left.size() != right.size()) {
11525 return false;
11526 }
11527 final Iterator it1 = left.iterator(), it2 = right.iterator();
11528 while (it1.hasNext()) {
11529 final Object o1 = it1.next();
11530 final Object o2 = it2.next();
11531 if (o1 == null) {
11532 if (o2 != null) return false;
11533 } else if (!coercedEquals(o1, o2)) {
11534 return false;
11535 }
11536 }
11537 return true;
11538 }
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566 public static <T> boolean equals(Set<T> self, Set<T> other) {
11567 if (self == null) {
11568 return other == null;
11569 }
11570 if (other == null) {
11571 return false;
11572 }
11573 if (self == other) {
11574 return true;
11575 }
11576 if (self.size() != other.size()) {
11577 return false;
11578 }
11579 final Iterator<T> it1 = self.iterator();
11580 Collection<T> otherItems = new HashSet<T>(other);
11581 while (it1.hasNext()) {
11582 final Object o1 = it1.next();
11583 final Iterator<T> it2 = otherItems.iterator();
11584 T foundItem = null;
11585 boolean found = false;
11586 while (it2.hasNext() && foundItem == null) {
11587 final T o2 = it2.next();
11588 if (coercedEquals(o1, o2)) {
11589 foundItem = o2;
11590 found = true;
11591 }
11592 }
11593 if (!found) return false;
11594 otherItems.remove(foundItem);
11595 }
11596 return otherItems.size() == 0;
11597 }
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610 public static boolean equals(Map self, Map other) {
11611 if (self == null) {
11612 return other == null;
11613 }
11614 if (other == null) {
11615 return false;
11616 }
11617 if (self == other) {
11618 return true;
11619 }
11620 if (self.size() != other.size()) {
11621 return false;
11622 }
11623 if (!self.keySet().equals(other.keySet())) {
11624 return false;
11625 }
11626 for (Object key : self.keySet()) {
11627 if (!coercedEquals(self.get(key), other.get(key))) {
11628 return false;
11629 }
11630 }
11631 return true;
11632 }
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643 public static <T> Set<T> minus(Set<T> self, Collection<?> removeMe) {
11644 Comparator comparator = (self instanceof SortedSet) ? ((SortedSet) self).comparator() : null;
11645 final Set<T> ansSet = createSimilarSet(self);
11646 ansSet.addAll(self);
11647 if (removeMe != null) {
11648 for (T o1 : self) {
11649 for (Object o2 : removeMe) {
11650 boolean areEqual = (comparator != null) ? (comparator.compare(o1, o2) == 0) : coercedEquals(o1, o2);
11651 if (areEqual) {
11652 ansSet.remove(o1);
11653 }
11654 }
11655 }
11656 }
11657 return ansSet;
11658 }
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669 public static <T> Set<T> minus(Set<T> self, Iterable<?> removeMe) {
11670 return minus(self, asCollection(removeMe));
11671 }
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681 public static <T> Set<T> minus(Set<T> self, Object removeMe) {
11682 Comparator comparator = (self instanceof SortedSet) ? ((SortedSet) self).comparator() : null;
11683 final Set<T> ansSet = createSimilarSet(self);
11684 for (T t : self) {
11685 boolean areEqual = (comparator != null)? (comparator.compare(t, removeMe) == 0) : coercedEquals(t, removeMe);
11686 if (!areEqual) ansSet.add(t);
11687 }
11688 return ansSet;
11689 }
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700 public static <T> SortedSet<T> minus(SortedSet<T> self, Collection<?> removeMe) {
11701 return (SortedSet<T>) minus((Set<T>) self, removeMe);
11702 }
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713 public static <T> SortedSet<T> minus(SortedSet<T> self, Iterable<?> removeMe) {
11714 return (SortedSet<T>) minus((Set<T>) self, removeMe);
11715 }
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725 public static <T> SortedSet<T> minus(SortedSet<T> self, Object removeMe) {
11726 return (SortedSet<T>) minus((Set<T>) self, removeMe);
11727 }
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738 @SuppressWarnings("unchecked")
11739 public static <T> T[] minus(T[] self, Iterable removeMe) {
11740 return (T[]) minus(toList(self), removeMe).toArray();
11741 }
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752 @SuppressWarnings("unchecked")
11753 public static <T> T[] minus(T[] self, Object[] removeMe) {
11754 return (T[]) minus(toList(self), toList(removeMe)).toArray();
11755 }
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767 public static <T> List<T> minus(List<T> self, Collection<?> removeMe) {
11768 return (List<T>) minus((Collection<T>) self, removeMe);
11769 }
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781 public static <T> Collection<T> minus(Collection<T> self, Collection<?> removeMe) {
11782 Collection<T> ansCollection = createSimilarCollection(self);
11783 if (self.size() == 0)
11784 return ansCollection;
11785 T head = self.iterator().next();
11786
11787 boolean nlgnSort = sameType(new Collection[]{self, removeMe});
11788
11789
11790
11791
11792
11793 Comparator<T> numberComparator = new NumberAwareComparator<T>();
11794
11795 if (nlgnSort && (head instanceof Comparable)) {
11796
11797 Set<T> answer;
11798 if (Number.class.isInstance(head)) {
11799 answer = new TreeSet<T>(numberComparator);
11800 answer.addAll(self);
11801 for (T t : self) {
11802 if (Number.class.isInstance(t)) {
11803 for (Object t2 : removeMe) {
11804 if (Number.class.isInstance(t2)) {
11805 if (numberComparator.compare(t, (T)t2) == 0)
11806 answer.remove(t);
11807 }
11808 }
11809 } else {
11810 if (removeMe.contains(t))
11811 answer.remove(t);
11812 }
11813 }
11814 } else {
11815 answer = new TreeSet<T>(numberComparator);
11816 answer.addAll(self);
11817 answer.removeAll(removeMe);
11818 }
11819
11820 for (T o : self) {
11821 if (answer.contains(o))
11822 ansCollection.add(o);
11823 }
11824 } else {
11825
11826 List<T> tmpAnswer = new LinkedList<T>(self);
11827 for (Iterator<T> iter = tmpAnswer.iterator(); iter.hasNext();) {
11828 T element = iter.next();
11829 boolean elementRemoved = false;
11830 for (Iterator<?> iterator = removeMe.iterator(); iterator.hasNext() && !elementRemoved;) {
11831 Object elt = iterator.next();
11832 if (DefaultTypeTransformation.compareEqual(element, elt)) {
11833 iter.remove();
11834 elementRemoved = true;
11835 }
11836 }
11837 }
11838
11839
11840
11841 ansCollection.addAll(tmpAnswer);
11842 }
11843 return ansCollection;
11844 }
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856 public static <T> List<T> minus(List<T> self, Iterable<?> removeMe) {
11857 return (List<T>) minus((Iterable<T>) self, removeMe);
11858 }
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872 public static <T> Collection<T> minus(Iterable<T> self, Iterable<?> removeMe) {
11873 return minus(asCollection(self), asCollection(removeMe));
11874 }
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886 public static <T> List<T> minus(List<T> self, Object removeMe) {
11887 return (List<T>) minus((Iterable<T>) self, removeMe);
11888 }
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900 public static <T> Collection<T> minus(Iterable<T> self, Object removeMe) {
11901 Collection<T> ansList = createSimilarCollection(self);
11902 for (T t : self) {
11903 if (!coercedEquals(t, removeMe)) ansList.add(t);
11904 }
11905 return ansList;
11906 }
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917 @SuppressWarnings("unchecked")
11918 public static <T> T[] minus(T[] self, Object removeMe) {
11919 return (T[]) minus((Iterable<T>) toList(self), removeMe).toArray();
11920 }
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931 public static <K,V> Map<K,V> minus(Map<K,V> self, Map removeMe) {
11932 final Map<K,V> ansMap = createSimilarMap(self);
11933 ansMap.putAll(self);
11934 if (removeMe != null && removeMe.size() > 0) {
11935 for (Map.Entry<K, V> e1 : self.entrySet()) {
11936 for (Object e2 : removeMe.entrySet()) {
11937 if (DefaultTypeTransformation.compareEqual(e1, e2)) {
11938 ansMap.remove(e1.getKey());
11939 }
11940 }
11941 }
11942 }
11943 return ansMap;
11944 }
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955 public static Collection<?> flatten(Collection<?> self) {
11956 return flatten(self, createSimilarCollection(self));
11957 }
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968 public static Collection<?> flatten(Iterable<?> self) {
11969 return flatten(self, createSimilarCollection(self));
11970 }
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981 public static List<?> flatten(List<?> self) {
11982 return (List<?>) flatten((Collection<?>) self);
11983 }
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994 public static Set<?> flatten(Set<?> self) {
11995 return (Set<?>) flatten((Collection<?>) self);
11996 }
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007 public static SortedSet<?> flatten(SortedSet<?> self) {
12008 return (SortedSet<?>) flatten((Collection<?>) self);
12009 }
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019 public static Collection flatten(Object[] self) {
12020 return flatten(toList(self), new ArrayList());
12021 }
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031 public static Collection flatten(boolean[] self) {
12032 return flatten(toList(self), new ArrayList());
12033 }
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043 public static Collection flatten(byte[] self) {
12044 return flatten(toList(self), new ArrayList());
12045 }
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055 public static Collection flatten(char[] self) {
12056 return flatten(toList(self), new ArrayList());
12057 }
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067 public static Collection flatten(short[] self) {
12068 return flatten(toList(self), new ArrayList());
12069 }
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079 public static Collection flatten(int[] self) {
12080 return flatten(toList(self), new ArrayList());
12081 }
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091 public static Collection flatten(long[] self) {
12092 return flatten(toList(self), new ArrayList());
12093 }
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103 public static Collection flatten(float[] self) {
12104 return flatten(toList(self), new ArrayList());
12105 }
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115 public static Collection flatten(double[] self) {
12116 return flatten(toList(self), new ArrayList());
12117 }
12118
12119 private static Collection flatten(Iterable elements, Collection addTo) {
12120 for (Object element : elements) {
12121 if (element instanceof Collection) {
12122 flatten((Collection) element, addTo);
12123 } else if (element != null && element.getClass().isArray()) {
12124 flatten(DefaultTypeTransformation.arrayAsCollection(element), addTo);
12125 } else {
12126
12127 addTo.add(element);
12128 }
12129 }
12130 return addTo;
12131 }
12132
12133
12134
12135
12136
12137
12138 @Deprecated
12139 public static <T> Collection<T> flatten(Collection<T> self, Closure<? extends T> flattenUsing) {
12140 return flatten(self, createSimilarCollection(self), flattenUsing);
12141 }
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155 public static <T> Collection<T> flatten(Iterable<T> self, Closure<? extends T> flattenUsing) {
12156 return flatten(self, createSimilarCollection(self), flattenUsing);
12157 }
12158
12159 private static <T> Collection<T> flatten(Iterable elements, Collection<T> addTo, Closure<? extends T> flattenUsing) {
12160 for (Object element : elements) {
12161 if (element instanceof Collection) {
12162 flatten((Collection) element, addTo, flattenUsing);
12163 } else if (element != null && element.getClass().isArray()) {
12164 flatten(DefaultTypeTransformation.arrayAsCollection(element), addTo, flattenUsing);
12165 } else {
12166 T flattened = flattenUsing.call(new Object[]{element});
12167 boolean returnedSelf = flattened == element;
12168 if (!returnedSelf && flattened instanceof Collection) {
12169 List<?> list = toList((Iterable<?>) flattened);
12170 if (list.size() == 1 && list.get(0) == element) {
12171 returnedSelf = true;
12172 }
12173 }
12174 if (flattened instanceof Collection && !returnedSelf) {
12175 flatten((Collection) flattened, addTo, flattenUsing);
12176 } else {
12177 addTo.add(flattened);
12178 }
12179 }
12180 }
12181 return addTo;
12182 }
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196 public static <T> Collection<T> leftShift(Collection<T> self, T value) {
12197 self.add(value);
12198 return self;
12199 }
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213 public static <T> List<T> leftShift(List<T> self, T value) {
12214 return (List<T>) leftShift((Collection<T>) self, value);
12215 }
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229 public static <T> Set<T> leftShift(Set<T> self, T value) {
12230 return (Set<T>) leftShift((Collection<T>) self, value);
12231 }
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245 public static <T> SortedSet<T> leftShift(SortedSet<T> self, T value) {
12246 return (SortedSet<T>) leftShift((Collection<T>) self, value);
12247 }
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262 public static <T> BlockingQueue<T> leftShift(BlockingQueue<T> self, T value) throws InterruptedException {
12263 self.put(value);
12264 return self;
12265 }
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276 public static <K, V> Map<K, V> leftShift(Map<K, V> self, Map.Entry<K, V> entry) {
12277 self.put(entry.getKey(), entry.getValue());
12278 return self;
12279 }
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296 public static <K, V> Map<K, V> leftShift(Map<K, V> self, Map<K, V> other) {
12297 self.putAll(other);
12298 return self;
12299 }
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310 public static Number leftShift(Number self, Number operand) {
12311 return NumberMath.leftShift(self, operand);
12312 }
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323 public static Number rightShift(Number self, Number operand) {
12324 return NumberMath.rightShift(self, operand);
12325 }
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336 public static Number rightShiftUnsigned(Number self, Number operand) {
12337 return NumberMath.rightShiftUnsigned(self, operand);
12338 }
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351 @SuppressWarnings("unchecked")
12352 public static List<Byte> getAt(byte[] array, Range range) {
12353 return primitiveArrayGet(array, range);
12354 }
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364 @SuppressWarnings("unchecked")
12365 public static List<Character> getAt(char[] array, Range range) {
12366 return primitiveArrayGet(array, range);
12367 }
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377 @SuppressWarnings("unchecked")
12378 public static List<Short> getAt(short[] array, Range range) {
12379 return primitiveArrayGet(array, range);
12380 }
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390 @SuppressWarnings("unchecked")
12391 public static List<Integer> getAt(int[] array, Range range) {
12392 return primitiveArrayGet(array, range);
12393 }
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403 @SuppressWarnings("unchecked")
12404 public static List<Long> getAt(long[] array, Range range) {
12405 return primitiveArrayGet(array, range);
12406 }
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416 @SuppressWarnings("unchecked")
12417 public static List<Float> getAt(float[] array, Range range) {
12418 return primitiveArrayGet(array, range);
12419 }
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429 @SuppressWarnings("unchecked")
12430 public static List<Double> getAt(double[] array, Range range) {
12431 return primitiveArrayGet(array, range);
12432 }
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442 @SuppressWarnings("unchecked")
12443 public static List<Boolean> getAt(boolean[] array, Range range) {
12444 return primitiveArrayGet(array, range);
12445 }
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455 @SuppressWarnings("unchecked")
12456 public static List<Byte> getAt(byte[] array, IntRange range) {
12457 RangeInfo info = subListBorders(array.length, range);
12458 List<Byte> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12459 return info.reverse ? reverse(answer) : answer;
12460 }
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470 @SuppressWarnings("unchecked")
12471 public static List<Character> getAt(char[] array, IntRange range) {
12472 RangeInfo info = subListBorders(array.length, range);
12473 List<Character> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12474 return info.reverse ? reverse(answer) : answer;
12475 }
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485 @SuppressWarnings("unchecked")
12486 public static List<Short> getAt(short[] array, IntRange range) {
12487 RangeInfo info = subListBorders(array.length, range);
12488 List<Short> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12489 return info.reverse ? reverse(answer) : answer;
12490 }
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500 @SuppressWarnings("unchecked")
12501 public static List<Integer> getAt(int[] array, IntRange range) {
12502 RangeInfo info = subListBorders(array.length, range);
12503 List<Integer> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12504 return info.reverse ? reverse(answer) : answer;
12505 }
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515 @SuppressWarnings("unchecked")
12516 public static List<Long> getAt(long[] array, IntRange range) {
12517 RangeInfo info = subListBorders(array.length, range);
12518 List<Long> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12519 return info.reverse ? reverse(answer) : answer;
12520 }
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530 @SuppressWarnings("unchecked")
12531 public static List<Float> getAt(float[] array, IntRange range) {
12532 RangeInfo info = subListBorders(array.length, range);
12533 List<Float> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12534 return info.reverse ? reverse(answer) : answer;
12535 }
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545 @SuppressWarnings("unchecked")
12546 public static List<Double> getAt(double[] array, IntRange range) {
12547 RangeInfo info = subListBorders(array.length, range);
12548 List<Double> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12549 return info.reverse ? reverse(answer) : answer;
12550 }
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560 @SuppressWarnings("unchecked")
12561 public static List<Boolean> getAt(boolean[] array, IntRange range) {
12562 RangeInfo info = subListBorders(array.length, range);
12563 List<Boolean> answer = primitiveArrayGet(array, new IntRange(true, info.from, info.to - 1));
12564 return info.reverse ? reverse(answer) : answer;
12565 }
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575 @SuppressWarnings("unchecked")
12576 public static List<Byte> getAt(byte[] array, ObjectRange range) {
12577 return primitiveArrayGet(array, range);
12578 }
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588 @SuppressWarnings("unchecked")
12589 public static List<Character> getAt(char[] array, ObjectRange range) {
12590 return primitiveArrayGet(array, range);
12591 }
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601 @SuppressWarnings("unchecked")
12602 public static List<Short> getAt(short[] array, ObjectRange range) {
12603 return primitiveArrayGet(array, range);
12604 }
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614 @SuppressWarnings("unchecked")
12615 public static List<Integer> getAt(int[] array, ObjectRange range) {
12616 return primitiveArrayGet(array, range);
12617 }
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627 @SuppressWarnings("unchecked")
12628 public static List<Long> getAt(long[] array, ObjectRange range) {
12629 return primitiveArrayGet(array, range);
12630 }
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640 @SuppressWarnings("unchecked")
12641 public static List<Float> getAt(float[] array, ObjectRange range) {
12642 return primitiveArrayGet(array, range);
12643 }
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653 @SuppressWarnings("unchecked")
12654 public static List<Double> getAt(double[] array, ObjectRange range) {
12655 return primitiveArrayGet(array, range);
12656 }
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666 @SuppressWarnings("unchecked")
12667 public static List<Boolean> getAt(boolean[] array, ObjectRange range) {
12668 return primitiveArrayGet(array, range);
12669 }
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679 @SuppressWarnings("unchecked")
12680 public static List<Byte> getAt(byte[] array, Collection indices) {
12681 return primitiveArrayGet(array, indices);
12682 }
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692 @SuppressWarnings("unchecked")
12693 public static List<Character> getAt(char[] array, Collection indices) {
12694 return primitiveArrayGet(array, indices);
12695 }
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705 @SuppressWarnings("unchecked")
12706 public static List<Short> getAt(short[] array, Collection indices) {
12707 return primitiveArrayGet(array, indices);
12708 }
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718 @SuppressWarnings("unchecked")
12719 public static List<Integer> getAt(int[] array, Collection indices) {
12720 return primitiveArrayGet(array, indices);
12721 }
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731 @SuppressWarnings("unchecked")
12732 public static List<Long> getAt(long[] array, Collection indices) {
12733 return primitiveArrayGet(array, indices);
12734 }
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744 @SuppressWarnings("unchecked")
12745 public static List<Float> getAt(float[] array, Collection indices) {
12746 return primitiveArrayGet(array, indices);
12747 }
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757 @SuppressWarnings("unchecked")
12758 public static List<Double> getAt(double[] array, Collection indices) {
12759 return primitiveArrayGet(array, indices);
12760 }
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770 @SuppressWarnings("unchecked")
12771 public static List<Boolean> getAt(boolean[] array, Collection indices) {
12772 return primitiveArrayGet(array, indices);
12773 }
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784 public static boolean getAt(BitSet self, int index) {
12785 int i = normaliseIndex(index, self.length());
12786 return self.get(i);
12787 }
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799 public static BitSet getAt(BitSet self, IntRange range) {
12800 RangeInfo info = subListBorders(self.length(), range);
12801 BitSet result = new BitSet();
12802
12803 int numberOfBits = info.to - info.from;
12804 int adjuster = 1;
12805 int offset = info.from;
12806
12807 if (info.reverse) {
12808 adjuster = -1;
12809 offset = info.to - 1;
12810 }
12811
12812 for (int i = 0; i < numberOfBits; i++) {
12813 result.set(i, self.get(offset + (adjuster * i)));
12814 }
12815
12816 return result;
12817 }
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892 public static void putAt(BitSet self, IntRange range, boolean value) {
12893 RangeInfo info = subListBorders(self.length(), range);
12894 self.set(info.from, info.to, value);
12895 }
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906 public static void putAt(BitSet self, int index, boolean value) {
12907 self.set(index, value);
12908 }
12909
12910
12911
12912
12913
12914
12915
12916
12917 public static int size(boolean[] array) {
12918 return Array.getLength(array);
12919 }
12920
12921
12922
12923
12924
12925
12926
12927
12928 public static int size(byte[] array) {
12929 return Array.getLength(array);
12930 }
12931
12932
12933
12934
12935
12936
12937
12938
12939 public static int size(char[] array) {
12940 return Array.getLength(array);
12941 }
12942
12943
12944
12945
12946
12947
12948
12949
12950 public static int size(short[] array) {
12951 return Array.getLength(array);
12952 }
12953
12954
12955
12956
12957
12958
12959
12960
12961 public static int size(int[] array) {
12962 return Array.getLength(array);
12963 }
12964
12965
12966
12967
12968
12969
12970
12971
12972 public static int size(long[] array) {
12973 return Array.getLength(array);
12974 }
12975
12976
12977
12978
12979
12980
12981
12982
12983 public static int size(float[] array) {
12984 return Array.getLength(array);
12985 }
12986
12987
12988
12989
12990
12991
12992
12993
12994 public static int size(double[] array) {
12995 return Array.getLength(array);
12996 }
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006 @SuppressWarnings("unchecked")
13007 public static List<Byte> toList(byte[] array) {
13008 return DefaultTypeTransformation.primitiveArrayToList(array);
13009 }
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019 @SuppressWarnings("unchecked")
13020 public static List<Boolean> toList(boolean[] array) {
13021 return DefaultTypeTransformation.primitiveArrayToList(array);
13022 }
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032 @SuppressWarnings("unchecked")
13033 public static List<Character> toList(char[] array) {
13034 return DefaultTypeTransformation.primitiveArrayToList(array);
13035 }
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045 @SuppressWarnings("unchecked")
13046 public static List<Short> toList(short[] array) {
13047 return DefaultTypeTransformation.primitiveArrayToList(array);
13048 }
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058 @SuppressWarnings("unchecked")
13059 public static List<Integer> toList(int[] array) {
13060 return DefaultTypeTransformation.primitiveArrayToList(array);
13061 }
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071 @SuppressWarnings("unchecked")
13072 public static List<Long> toList(long[] array) {
13073 return DefaultTypeTransformation.primitiveArrayToList(array);
13074 }
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084 @SuppressWarnings("unchecked")
13085 public static List<Float> toList(float[] array) {
13086 return DefaultTypeTransformation.primitiveArrayToList(array);
13087 }
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097 @SuppressWarnings("unchecked")
13098 public static List<Double> toList(double[] array) {
13099 return DefaultTypeTransformation.primitiveArrayToList(array);
13100 }
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110 @SuppressWarnings("unchecked")
13111 public static Set<Byte> toSet(byte[] array) {
13112 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13113 }
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123 @SuppressWarnings("unchecked")
13124 public static Set<Boolean> toSet(boolean[] array) {
13125 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13126 }
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136 @SuppressWarnings("unchecked")
13137 public static Set<Character> toSet(char[] array) {
13138 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13139 }
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149 @SuppressWarnings("unchecked")
13150 public static Set<Short> toSet(short[] array) {
13151 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13152 }
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162 @SuppressWarnings("unchecked")
13163 public static Set<Integer> toSet(int[] array) {
13164 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13165 }
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175 @SuppressWarnings("unchecked")
13176 public static Set<Long> toSet(long[] array) {
13177 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13178 }
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188 @SuppressWarnings("unchecked")
13189 public static Set<Float> toSet(float[] array) {
13190 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13191 }
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201 @SuppressWarnings("unchecked")
13202 public static Set<Double> toSet(double[] array) {
13203 return toSet(DefaultTypeTransformation.primitiveArrayToList(array));
13204 }
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221 public static <T> Set<T> toSet(Collection<T> self) {
13222 Set<T> answer = new HashSet<T>(self.size());
13223 answer.addAll(self);
13224 return answer;
13225 }
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242 public static <T> Set<T> toSet(Iterable<T> self) {
13243 return toSet(self.iterator());
13244 }
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254 public static <T> Set<T> toSet(Iterator<T> self) {
13255 Set<T> answer = new HashSet<T>();
13256 while (self.hasNext()) {
13257 answer.add(self.next());
13258 }
13259 return answer;
13260 }
13261
13262
13263
13264
13265
13266
13267
13268
13269 public static <T> Set<T> toSet(Enumeration<T> self) {
13270 Set<T> answer = new HashSet<T>();
13271 while (self.hasMoreElements()) {
13272 answer.add(self.nextElement());
13273 }
13274 return answer;
13275 }
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285 protected static Object primitiveArrayGet(Object self, int idx) {
13286 return Array.get(self, normaliseIndex(idx, Array.getLength(self)));
13287 }
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297 protected static List primitiveArrayGet(Object self, Range range) {
13298 List answer = new ArrayList();
13299 for (Object next : range) {
13300 int idx = DefaultTypeTransformation.intUnbox(next);
13301 answer.add(primitiveArrayGet(self, idx));
13302 }
13303 return answer;
13304 }
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316 protected static List primitiveArrayGet(Object self, Collection indices) {
13317 List answer = new ArrayList();
13318 for (Object value : indices) {
13319 if (value instanceof Range) {
13320 answer.addAll(primitiveArrayGet(self, (Range) value));
13321 } else if (value instanceof List) {
13322 answer.addAll(primitiveArrayGet(self, (List) value));
13323 } else {
13324 int idx = DefaultTypeTransformation.intUnbox(value);
13325 answer.add(primitiveArrayGet(self, idx));
13326 }
13327 }
13328 return answer;
13329 }
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340 protected static Object primitiveArrayPut(Object self, int idx, Object newValue) {
13341 Array.set(self, normaliseIndex(idx, Array.getLength(self)), newValue);
13342 return newValue;
13343 }
13344
13345
13346
13347
13348
13349
13350
13351
13352 public static Boolean toBoolean(Boolean self) {
13353 return self;
13354 }
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364 public static boolean contains(int[] self, Object value) {
13365 for (int next : self) {
13366 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13367 }
13368 return false;
13369 }
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379 public static boolean contains(long[] self, Object value) {
13380 for (long next : self) {
13381 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13382 }
13383 return false;
13384 }
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394 public static boolean contains(short[] self, Object value) {
13395 for (short next : self) {
13396 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13397 }
13398 return false;
13399 }
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409 public static boolean contains(char[] self, Object value) {
13410 for (char next : self) {
13411 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13412 }
13413 return false;
13414 }
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424 public static boolean contains(boolean[] self, Object value) {
13425 for (boolean next : self) {
13426 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13427 }
13428 return false;
13429 }
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439 public static boolean contains(double[] self, Object value) {
13440 for (double next : self) {
13441 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13442 }
13443 return false;
13444 }
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454 public static boolean contains(float[] self, Object value) {
13455 for (float next : self) {
13456 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13457 }
13458 return false;
13459 }
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469 public static boolean contains(byte[] self, Object value) {
13470 for (byte next : self) {
13471 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13472 }
13473 return false;
13474 }
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484 public static boolean contains(Object[] self, Object value) {
13485 for (Object next : self) {
13486 if (DefaultTypeTransformation.compareEqual(value, next)) return true;
13487 }
13488 return false;
13489 }
13490
13491
13492
13493
13494
13495
13496
13497
13498 public static String toString(boolean[] self) {
13499 return InvokerHelper.toString(self);
13500 }
13501
13502
13503
13504
13505
13506
13507
13508
13509 public static String toString(byte[] self) {
13510 return InvokerHelper.toString(self);
13511 }
13512
13513
13514
13515
13516
13517
13518
13519
13520 public static String toString(char[] self) {
13521 return InvokerHelper.toString(self);
13522 }
13523
13524
13525
13526
13527
13528
13529
13530
13531 public static String toString(short[] self) {
13532 return InvokerHelper.toString(self);
13533 }
13534
13535
13536
13537
13538
13539
13540
13541
13542 public static String toString(int[] self) {
13543 return InvokerHelper.toString(self);
13544 }
13545
13546
13547
13548
13549
13550
13551
13552
13553 public static String toString(long[] self) {
13554 return InvokerHelper.toString(self);
13555 }
13556
13557
13558
13559
13560
13561
13562
13563
13564 public static String toString(float[] self) {
13565 return InvokerHelper.toString(self);
13566 }
13567
13568
13569
13570
13571
13572
13573
13574
13575 public static String toString(double[] self) {
13576 return InvokerHelper.toString(self);
13577 }
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587 public static String toString(AbstractMap self) {
13588 return toMapString(self);
13589 }
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599 public static String toMapString(Map self) {
13600 return toMapString(self, -1);
13601 }
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612 public static String toMapString(Map self, int maxSize) {
13613 return (self == null) ? "null" : InvokerHelper.toMapString(self, maxSize);
13614 }
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626 public static String toString(AbstractCollection self) {
13627 return toListString(self);
13628 }
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639 public static String toListString(Collection self) {
13640 return toListString(self, -1);
13641 }
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653 public static String toListString(Collection self, int maxSize) {
13654 return (self == null) ? "null" : InvokerHelper.toListString(self, maxSize);
13655 }
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665 public static String toString(Object[] self) {
13666 return toArrayString(self);
13667 }
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678 public static String toArrayString(Object[] self) {
13679 return (self == null) ? "null" : InvokerHelper.toArrayString(self);
13680 }
13681
13682
13683
13684
13685
13686
13687
13688 public static String toString(Object value) {
13689 return InvokerHelper.toString(value);
13690 }
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702 public static Character next(Character self) {
13703 return (char) (self + 1);
13704 }
13705
13706
13707
13708
13709
13710
13711
13712
13713 public static Number next(Number self) {
13714 return NumberNumberPlus.plus(self, ONE);
13715 }
13716
13717
13718
13719
13720
13721
13722
13723
13724 public static Character previous(Character self) {
13725 return (char) (self - 1);
13726 }
13727
13728
13729
13730
13731
13732
13733
13734
13735 public static Number previous(Number self) {
13736 return NumberNumberMinus.minus(self, ONE);
13737 }
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752 public static Number plus(Character left, Number right) {
13753 return NumberNumberPlus.plus(Integer.valueOf(left), right);
13754 }
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767 public static Number plus(Number left, Character right) {
13768 return NumberNumberPlus.plus(left, Integer.valueOf(right));
13769 }
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784 public static Number plus(Character left, Character right) {
13785 return plus(Integer.valueOf(left), right);
13786 }
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798 public static int compareTo(Character left, Number right) {
13799 return compareTo(Integer.valueOf(left), right);
13800 }
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812 public static int compareTo(Number left, Character right) {
13813 return compareTo(left, Integer.valueOf(right));
13814 }
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826 public static int compareTo(Character left, Character right) {
13827 return compareTo(Integer.valueOf(left), right);
13828 }
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838 public static int compareTo(Number left, Number right) {
13839
13840 return NumberMath.compareTo(left, right);
13841 }
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853 public static Number minus(Character left, Number right) {
13854 return NumberNumberMinus.minus(Integer.valueOf(left), right);
13855 }
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867 public static Number minus(Number left, Character right) {
13868 return NumberNumberMinus.minus(left, Integer.valueOf(right));
13869 }
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881 public static Number minus(Character left, Character right) {
13882 return minus(Integer.valueOf(left), right);
13883 }
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895 public static Number multiply(Character left, Number right) {
13896 return NumberNumberMultiply.multiply(Integer.valueOf(left), right);
13897 }
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909 public static Number multiply(Number left, Character right) {
13910 return NumberNumberMultiply.multiply(Integer.valueOf(right), left);
13911 }
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923 public static Number multiply(Character left, Character right) {
13924 return multiply(Integer.valueOf(left), right);
13925 }
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941 public static Number multiply(BigDecimal left, Double right) {
13942 return NumberMath.multiply(left, right);
13943 }
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960 public static Number multiply(BigDecimal left, BigInteger right) {
13961 return NumberMath.multiply(left, right);
13962 }
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972 public static Number power(Number self, Number exponent) {
13973 double base, exp, answer;
13974 base = self.doubleValue();
13975 exp = exponent.doubleValue();
13976
13977 answer = Math.pow(base, exp);
13978 if ((double) ((int) answer) == answer) {
13979 return (int) answer;
13980 } else if ((double) ((long) answer) == answer) {
13981 return (long) answer;
13982 } else {
13983 return answer;
13984 }
13985 }
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996 public static Number power(BigDecimal self, Integer exponent) {
13997 if (exponent >= 0) {
13998 return self.pow(exponent);
13999 } else {
14000 return power(self, (double) exponent);
14001 }
14002 }
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013 public static Number power(BigInteger self, Integer exponent) {
14014 if (exponent >= 0) {
14015 return self.pow(exponent);
14016 } else {
14017 return power(self, (double) exponent);
14018 }
14019 }
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031 public static Number power(Integer self, Integer exponent) {
14032 if (exponent >= 0) {
14033 BigInteger answer = BigInteger.valueOf(self).pow(exponent);
14034 if (answer.compareTo(BI_INT_MIN) >= 0 && answer.compareTo(BI_INT_MAX) <= 0) {
14035 return answer.intValue();
14036 } else {
14037 return answer;
14038 }
14039 } else {
14040 return power(self, (double) exponent);
14041 }
14042 }
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054 public static Number power(Long self, Integer exponent) {
14055 if (exponent >= 0) {
14056 BigInteger answer = BigInteger.valueOf(self).pow(exponent);
14057 if (answer.compareTo(BI_LONG_MIN) >= 0 && answer.compareTo(BI_LONG_MAX) <= 0) {
14058 return answer.longValue();
14059 } else {
14060 return answer;
14061 }
14062 } else {
14063 return power(self, (double) exponent);
14064 }
14065 }
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076 public static BigInteger power(BigInteger self, BigInteger exponent) {
14077 if ((exponent.signum() >= 0) && (exponent.compareTo(BI_INT_MAX) <= 0)) {
14078 return self.pow(exponent.intValue());
14079 } else {
14080 return BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger();
14081 }
14082 }
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094 public static Number div(Character left, Number right) {
14095 return NumberNumberDiv.div(Integer.valueOf(left), right);
14096 }
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108 public static Number div(Number left, Character right) {
14109 return NumberNumberDiv.div(left, Integer.valueOf(right));
14110 }
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122 public static Number div(Character left, Character right) {
14123 return div(Integer.valueOf(left), right);
14124 }
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136 public static Number intdiv(Character left, Number right) {
14137 return intdiv(Integer.valueOf(left), right);
14138 }
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150 public static Number intdiv(Number left, Character right) {
14151 return intdiv(left, Integer.valueOf(right));
14152 }
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164 public static Number intdiv(Character left, Character right) {
14165 return intdiv(Integer.valueOf(left), right);
14166 }
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176 public static Number intdiv(Number left, Number right) {
14177 return NumberMath.intdiv(left, right);
14178 }
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188 public static Number or(Number left, Number right) {
14189 return NumberMath.or(left, right);
14190 }
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200 public static Number and(Number left, Number right) {
14201 return NumberMath.and(left, right);
14202 }
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212 public static BitSet and(BitSet left, BitSet right) {
14213 BitSet result = (BitSet) left.clone();
14214 result.and(right);
14215 return result;
14216 }
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227 public static BitSet xor(BitSet left, BitSet right) {
14228 BitSet result = (BitSet) left.clone();
14229 result.xor(right);
14230 return result;
14231 }
14232
14233
14234
14235
14236
14237
14238
14239
14240 public static BitSet bitwiseNegate(BitSet self) {
14241 BitSet result = (BitSet) self.clone();
14242 result.flip(0, result.size() - 1);
14243 return result;
14244 }
14245
14246
14247
14248
14249
14250
14251
14252
14253 public static Number bitwiseNegate(Number left) {
14254 return NumberMath.bitwiseNegate(left);
14255 }
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266 public static BitSet or(BitSet left, BitSet right) {
14267 BitSet result = (BitSet) left.clone();
14268 result.or(right);
14269 return result;
14270 }
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280 public static Number xor(Number left, Number right) {
14281 return NumberMath.xor(left, right);
14282 }
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292 public static Number mod(Number left, Number right) {
14293 return NumberMath.mod(left, right);
14294 }
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304 public static Number unaryMinus(Number left) {
14305 return NumberMath.unaryMinus(left);
14306 }
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317 public static Number unaryPlus(Number left) {
14318 return NumberMath.unaryPlus(left);
14319 }
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334 public static void times(Number self, @ClosureParams(value=SimpleType.class,options="int") Closure closure) {
14335 for (int i = 0, size = self.intValue(); i < size; i++) {
14336 closure.call(i);
14337 if (closure.getDirective() == Closure.DONE) {
14338 break;
14339 }
14340 }
14341 }
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352 public static void upto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14353 int self1 = self.intValue();
14354 int to1 = to.intValue();
14355 if (self1 <= to1) {
14356 for (int i = self1; i <= to1; i++) {
14357 closure.call(i);
14358 }
14359 } else
14360 throw new GroovyRuntimeException("The argument (" + to +
14361 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14362 }
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373 public static void upto(long self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14374 long to1 = to.longValue();
14375 if (self <= to1) {
14376 for (long i = self; i <= to1; i++) {
14377 closure.call(i);
14378 }
14379 } else
14380 throw new GroovyRuntimeException("The argument (" + to +
14381 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14382 }
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393 public static void upto(Long self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14394 long to1 = to.longValue();
14395 if (self <= to1) {
14396 for (long i = self; i <= to1; i++) {
14397 closure.call(i);
14398 }
14399 } else
14400 throw new GroovyRuntimeException("The argument (" + to +
14401 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14402 }
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413 public static void upto(float self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14414 float to1 = to.floatValue();
14415 if (self <= to1) {
14416 for (float i = self; i <= to1; i++) {
14417 closure.call(i);
14418 }
14419 } else
14420 throw new GroovyRuntimeException("The argument (" + to +
14421 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14422 }
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433 public static void upto(Float self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14434 float to1 = to.floatValue();
14435 if (self <= to1) {
14436 for (float i = self; i <= to1; i++) {
14437 closure.call(i);
14438 }
14439 } else
14440 throw new GroovyRuntimeException("The argument (" + to +
14441 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14442 }
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453 public static void upto(double self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14454 double to1 = to.doubleValue();
14455 if (self <= to1) {
14456 for (double i = self; i <= to1; i++) {
14457 closure.call(i);
14458 }
14459 } else
14460 throw new GroovyRuntimeException("The argument (" + to +
14461 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14462 }
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473 public static void upto(Double self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14474 double to1 = to.doubleValue();
14475 if (self <= to1) {
14476 for (double i = self; i <= to1; i++) {
14477 closure.call(i);
14478 }
14479 } else
14480 throw new GroovyRuntimeException("The argument (" + to +
14481 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14482 }
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497 public static void upto(BigInteger self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14498 if (to instanceof BigDecimal) {
14499 final BigDecimal one = BigDecimal.valueOf(10, 1);
14500 BigDecimal self1 = new BigDecimal(self);
14501 BigDecimal to1 = (BigDecimal) to;
14502 if (self1.compareTo(to1) <= 0) {
14503 for (BigDecimal i = self1; i.compareTo(to1) <= 0; i = i.add(one)) {
14504 closure.call(i);
14505 }
14506 } else
14507 throw new GroovyRuntimeException(
14508 MessageFormat.format(
14509 "The argument ({0}) to upto() cannot be less than the value ({1}) it''s called on.",
14510 to, self));
14511 } else if (to instanceof BigInteger) {
14512 final BigInteger one = BigInteger.valueOf(1);
14513 BigInteger to1 = (BigInteger) to;
14514 if (self.compareTo(to1) <= 0) {
14515 for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
14516 closure.call(i);
14517 }
14518 } else
14519 throw new GroovyRuntimeException(
14520 MessageFormat.format("The argument ({0}) to upto() cannot be less than the value ({1}) it''s called on.",
14521 to, self));
14522 } else {
14523 final BigInteger one = BigInteger.valueOf(1);
14524 BigInteger to1 = new BigInteger(to.toString());
14525 if (self.compareTo(to1) <= 0) {
14526 for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
14527 closure.call(i);
14528 }
14529 } else
14530 throw new GroovyRuntimeException(MessageFormat.format(
14531 "The argument ({0}) to upto() cannot be less than the value ({1}) it''s called on.",
14532 to, self));
14533 }
14534 }
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549 public static void upto(BigDecimal self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14550 final BigDecimal one = BigDecimal.valueOf(10, 1);
14551 if (to instanceof BigDecimal) {
14552 BigDecimal to1 = (BigDecimal) to;
14553 if (self.compareTo(to1) <= 0) {
14554 for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
14555 closure.call(i);
14556 }
14557 } else
14558 throw new GroovyRuntimeException("The argument (" + to +
14559 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14560 } else if (to instanceof BigInteger) {
14561 BigDecimal to1 = new BigDecimal((BigInteger) to);
14562 if (self.compareTo(to1) <= 0) {
14563 for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
14564 closure.call(i);
14565 }
14566 } else
14567 throw new GroovyRuntimeException("The argument (" + to +
14568 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14569 } else {
14570 BigDecimal to1 = new BigDecimal(to.toString());
14571 if (self.compareTo(to1) <= 0) {
14572 for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
14573 closure.call(i);
14574 }
14575 } else
14576 throw new GroovyRuntimeException("The argument (" + to +
14577 ") to upto() cannot be less than the value (" + self + ") it's called on.");
14578 }
14579 }
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590 public static void downto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14591 int self1 = self.intValue();
14592 int to1 = to.intValue();
14593 if (self1 >= to1) {
14594 for (int i = self1; i >= to1; i--) {
14595 closure.call(i);
14596 }
14597 } else
14598 throw new GroovyRuntimeException("The argument (" + to +
14599 ") to downto() cannot be greater than the value (" + self + ") it's called on.");
14600 }
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611 public static void downto(long self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14612 long to1 = to.longValue();
14613 if (self >= to1) {
14614 for (long i = self; i >= to1; i--) {
14615 closure.call(i);
14616 }
14617 } else
14618 throw new GroovyRuntimeException("The argument (" + to +
14619 ") to downto() cannot be greater than the value (" + self + ") it's called on.");
14620 }
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631 public static void downto(Long self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14632 long to1 = to.longValue();
14633 if (self >= to1) {
14634 for (long i = self; i >= to1; i--) {
14635 closure.call(i);
14636 }
14637 } else
14638 throw new GroovyRuntimeException("The argument (" + to +
14639 ") to downto() cannot be greater than the value (" + self + ") it's called on.");
14640 }
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651 public static void downto(float self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14652 float to1 = to.floatValue();
14653 if (self >= to1) {
14654 for (float i = self; i >= to1; i--) {
14655 closure.call(i);
14656 }
14657 } else
14658 throw new GroovyRuntimeException("The argument (" + to +
14659 ") to downto() cannot be greater than the value (" + self + ") it's called on."); }
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670 public static void downto(Float self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14671 float to1 = to.floatValue();
14672 if (self >= to1) {
14673 for (float i = self; i >= to1; i--) {
14674 closure.call(i);
14675 }
14676 } else
14677 throw new GroovyRuntimeException("The argument (" + to +
14678 ") to downto() cannot be greater than the value (" + self + ") it's called on."); }
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689 public static void downto(double self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14690 double to1 = to.doubleValue();
14691 if (self >= to1) {
14692 for (double i = self; i >= to1; i--) {
14693 closure.call(i);
14694 }
14695 } else
14696 throw new GroovyRuntimeException("The argument (" + to +
14697 ") to downto() cannot be greater than the value (" + self + ") it's called on."); }
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708 public static void downto(Double self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14709 double to1 = to.doubleValue();
14710 if (self >= to1) {
14711 for (double i = self; i >= to1; i--) {
14712 closure.call(i);
14713 }
14714 } else
14715 throw new GroovyRuntimeException("The argument (" + to +
14716 ") to downto() cannot be greater than the value (" + self + ") it's called on."); }
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727 public static void downto(BigInteger self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14728 if (to instanceof BigDecimal) {
14729 final BigDecimal one = BigDecimal.valueOf(10, 1);
14730 final BigDecimal to1 = (BigDecimal) to;
14731 final BigDecimal selfD = new BigDecimal(self);
14732 if (selfD.compareTo(to1) >= 0) {
14733 for (BigDecimal i = selfD; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14734 closure.call(i.toBigInteger());
14735 }
14736 } else
14737 throw new GroovyRuntimeException(
14738 MessageFormat.format(
14739 "The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.",
14740 to, self));
14741 } else if (to instanceof BigInteger) {
14742 final BigInteger one = BigInteger.valueOf(1);
14743 final BigInteger to1 = (BigInteger) to;
14744 if (self.compareTo(to1) >= 0) {
14745 for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14746 closure.call(i);
14747 }
14748 } else
14749 throw new GroovyRuntimeException(
14750 MessageFormat.format(
14751 "The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.",
14752 to, self));
14753 } else {
14754 final BigInteger one = BigInteger.valueOf(1);
14755 final BigInteger to1 = new BigInteger(to.toString());
14756 if (self.compareTo(to1) >= 0) {
14757 for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14758 closure.call(i);
14759 }
14760 } else
14761 throw new GroovyRuntimeException(
14762 MessageFormat.format("The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.",
14763 to, self));
14764 }
14765 }
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781 public static void downto(BigDecimal self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
14782 final BigDecimal one = BigDecimal.valueOf(10, 1);
14783 if (to instanceof BigDecimal) {
14784 BigDecimal to1 = (BigDecimal) to;
14785 if (self.compareTo(to1) >= 0) {
14786 for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14787 closure.call(i);
14788 }
14789 } else
14790 throw new GroovyRuntimeException("The argument (" + to +
14791 ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else if (to instanceof BigInteger) {
14792 BigDecimal to1 = new BigDecimal((BigInteger) to);
14793 if (self.compareTo(to1) >= 0) {
14794 for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14795 closure.call(i);
14796 }
14797 } else
14798 throw new GroovyRuntimeException("The argument (" + to +
14799 ") to downto() cannot be greater than the value (" + self + ") it's called on."); } else {
14800 BigDecimal to1 = new BigDecimal(to.toString());
14801 if (self.compareTo(to1) >= 0) {
14802 for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
14803 closure.call(i);
14804 }
14805 } else
14806 throw new GroovyRuntimeException("The argument (" + to +
14807 ") to downto() cannot be greater than the value (" + self + ") it's called on."); }
14808 }
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824 public static void step(Number self, Number to, Number stepNumber, Closure closure) {
14825 if (self instanceof BigDecimal || to instanceof BigDecimal || stepNumber instanceof BigDecimal) {
14826 final BigDecimal zero = BigDecimal.valueOf(0, 1);
14827 BigDecimal self1 = (self instanceof BigDecimal) ? (BigDecimal) self : new BigDecimal(self.toString());
14828 BigDecimal to1 = (to instanceof BigDecimal) ? (BigDecimal) to : new BigDecimal(to.toString());
14829 BigDecimal stepNumber1 = (stepNumber instanceof BigDecimal) ? (BigDecimal) stepNumber : new BigDecimal(stepNumber.toString());
14830 if (stepNumber1.compareTo(zero) > 0 && to1.compareTo(self1) > 0) {
14831 for (BigDecimal i = self1; i.compareTo(to1) < 0; i = i.add(stepNumber1)) {
14832 closure.call(i);
14833 }
14834 } else if (stepNumber1.compareTo(zero) < 0 && to1.compareTo(self1) < 0) {
14835 for (BigDecimal i = self1; i.compareTo(to1) > 0; i = i.add(stepNumber1)) {
14836 closure.call(i);
14837 }
14838 } else if(self1.compareTo(to1) != 0)
14839 throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
14840 } else if (self instanceof BigInteger || to instanceof BigInteger || stepNumber instanceof BigInteger) {
14841 final BigInteger zero = BigInteger.valueOf(0);
14842 BigInteger self1 = (self instanceof BigInteger) ? (BigInteger) self : new BigInteger(self.toString());
14843 BigInteger to1 = (to instanceof BigInteger) ? (BigInteger) to : new BigInteger(to.toString());
14844 BigInteger stepNumber1 = (stepNumber instanceof BigInteger) ? (BigInteger) stepNumber : new BigInteger(stepNumber.toString());
14845 if (stepNumber1.compareTo(zero) > 0 && to1.compareTo(self1) > 0) {
14846 for (BigInteger i = self1; i.compareTo(to1) < 0; i = i.add(stepNumber1)) {
14847 closure.call(i);
14848 }
14849 } else if (stepNumber1.compareTo(zero) < 0 && to1.compareTo(self1) < 0) {
14850 for (BigInteger i = self1; i.compareTo(to1) > 0; i = i.add(stepNumber1)) {
14851 closure.call(i);
14852 }
14853 } else if(self1.compareTo(to1) != 0)
14854 throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
14855 } else {
14856 int self1 = self.intValue();
14857 int to1 = to.intValue();
14858 int stepNumber1 = stepNumber.intValue();
14859 if (stepNumber1 > 0 && to1 > self1) {
14860 for (int i = self1; i < to1; i += stepNumber1) {
14861 closure.call(i);
14862 }
14863 } else if (stepNumber1 < 0 && to1 < self1) {
14864 for (int i = self1; i > to1; i += stepNumber1) {
14865 closure.call(i);
14866 }
14867 } else if(self1 != to1)
14868 throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
14869 }
14870 }
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881 public static int abs(Number number) {
14882 return Math.abs(number.intValue());
14883 }
14884
14885
14886
14887
14888
14889
14890
14891
14892 public static long abs(Long number) {
14893 return Math.abs(number.longValue());
14894 }
14895
14896
14897
14898
14899
14900
14901
14902
14903 public static float abs(Float number) {
14904 return Math.abs(number.floatValue());
14905 }
14906
14907
14908
14909
14910
14911
14912
14913
14914 public static double abs(Double number) {
14915 return Math.abs(number);
14916 }
14917
14918
14919
14920
14921
14922
14923
14924
14925 public static int round(Float number) {
14926 return Math.round(number.floatValue());
14927 }
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937 public static float round(Float number, int precision) {
14938 return (float)(Math.floor(number.doubleValue()*Math.pow(10,precision)+0.5)/Math.pow(10,precision));
14939 }
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949 public static float trunc(Float number, int precision) {
14950 return (float)(Math.floor(number.doubleValue()*Math.pow(10,precision))/Math.pow(10,precision));
14951 }
14952
14953
14954
14955
14956
14957
14958
14959
14960 public static float trunc(Float number) {
14961 return (float)Math.floor(number.doubleValue());
14962 }
14963
14964
14965
14966
14967
14968
14969
14970
14971 public static long round(Double number) {
14972 return Math.round(number);
14973 }
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983 public static double round(Double number, int precision) {
14984 return Math.floor(number *Math.pow(10,precision)+0.5)/Math.pow(10,precision);
14985 }
14986
14987
14988
14989
14990
14991
14992
14993
14994 public static double trunc(Double number) {
14995 return Math.floor(number);
14996 }
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006 public static double trunc(Double number, int precision) {
15007 return Math.floor(number *Math.pow(10,precision))/Math.pow(10,precision);
15008 }
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019 public static boolean isUpperCase(Character self) {
15020 return Character.isUpperCase(self);
15021 }
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032 public static boolean isLowerCase(Character self) {
15033 return Character.isLowerCase(self);
15034 }
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045 public static boolean isLetter(Character self) {
15046 return Character.isLetter(self);
15047 }
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058 public static boolean isDigit(Character self) {
15059 return Character.isDigit(self);
15060 }
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071 public static boolean isLetterOrDigit(Character self) {
15072 return Character.isLetterOrDigit(self);
15073 }
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084 public static boolean isWhitespace(Character self) {
15085 return Character.isWhitespace(self);
15086 }
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099 public static char toUpperCase(Character self) {
15100 return Character.toUpperCase(self);
15101 }
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114 public static char toLowerCase(Character self) {
15115 return Character.toLowerCase(self);
15116 }
15117
15118
15119
15120
15121
15122
15123
15124
15125 public static Integer toInteger(Number self) {
15126 return self.intValue();
15127 }
15128
15129
15130
15131
15132
15133
15134
15135
15136 public static Long toLong(Number self) {
15137 return self.longValue();
15138 }
15139
15140
15141
15142
15143
15144
15145
15146
15147 public static Float toFloat(Number self) {
15148 return self.floatValue();
15149 }
15150
15151
15152
15153
15154
15155
15156
15157
15158 public static Double toDouble(Number self) {
15159
15160 if ((self instanceof Double)
15161 || (self instanceof Long)
15162 || (self instanceof Integer)
15163 || (self instanceof Short)
15164 || (self instanceof Byte))
15165 {
15166 return self.doubleValue();
15167 }
15168
15169
15170
15171
15172
15173
15174
15175
15176 return Double.valueOf(self.toString());
15177 }
15178
15179
15180
15181
15182
15183
15184
15185
15186 public static BigDecimal toBigDecimal(Number self) {
15187
15188 if ((self instanceof Long)
15189 || (self instanceof Integer)
15190 || (self instanceof Short)
15191 || (self instanceof Byte))
15192 {
15193 return BigDecimal.valueOf(self.longValue());
15194 }
15195
15196 return new BigDecimal(self.toString());
15197 }
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214 @SuppressWarnings("unchecked")
15215 public static <T> T asType(Number self, Class<T> c) {
15216 if (c == BigDecimal.class) {
15217 return (T) toBigDecimal(self);
15218 } else if (c == BigInteger.class) {
15219 return (T) toBigInteger(self);
15220 } else if (c == Double.class) {
15221 return (T) toDouble(self);
15222 } else if (c == Float.class) {
15223 return (T) toFloat(self);
15224 }
15225 return asType((Object) self, c);
15226 }
15227
15228
15229
15230
15231
15232
15233
15234
15235 public static BigInteger toBigInteger(Number self) {
15236 if (self instanceof BigInteger) {
15237 return (BigInteger) self;
15238 } else if (self instanceof BigDecimal) {
15239 return ((BigDecimal) self).toBigInteger();
15240 } else if (self instanceof Double) {
15241 return new BigDecimal((Double)self).toBigInteger();
15242 } else if (self instanceof Float) {
15243 return new BigDecimal((Float)self).toBigInteger();
15244 } else {
15245 return new BigInteger(Long.toString(self.longValue()));
15246 }
15247 }
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261 public static Boolean and(Boolean left, Boolean right) {
15262 return left && right;
15263 }
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273 public static Boolean or(Boolean left, Boolean right) {
15274 return left || right;
15275 }
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285 public static Boolean implies(Boolean left, Boolean right) {
15286 return !left || right;
15287 }
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297 public static Boolean xor(Boolean left, Boolean right) {
15298 return left ^ right;
15299 }
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315 public static TimerTask runAfter(Timer timer, int delay, final Closure closure) {
15316 TimerTask timerTask = new TimerTask() {
15317 public void run() {
15318 closure.call();
15319 }
15320 };
15321 timer.schedule(timerTask, delay);
15322 return timerTask;
15323 }
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333 public static void eachByte(Byte[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
15334 each(self, closure);
15335 }
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345 public static void eachByte(byte[] self, @ClosureParams(FirstParam.Component.class) Closure closure) {
15346 each(self, closure);
15347 }
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358 public static int findIndexOf(Object self, Closure closure) {
15359 return findIndexOf(self, 0, closure);
15360 }
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373 public static int findIndexOf(Object self, int startIndex, Closure closure) {
15374 int result = -1;
15375 int i = 0;
15376 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
15377 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); i++) {
15378 Object value = iter.next();
15379 if (i < startIndex) {
15380 continue;
15381 }
15382 if (bcw.call(value)) {
15383 result = i;
15384 break;
15385 }
15386 }
15387 return result;
15388 }
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399 public static int findLastIndexOf(Object self, Closure closure) {
15400 return findLastIndexOf(self, 0, closure);
15401 }
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414 public static int findLastIndexOf(Object self, int startIndex, Closure closure) {
15415 int result = -1;
15416 int i = 0;
15417 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
15418 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); i++) {
15419 Object value = iter.next();
15420 if (i < startIndex) {
15421 continue;
15422 }
15423 if (bcw.call(value)) {
15424 result = i;
15425 }
15426 }
15427 return result;
15428 }
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439 public static List<Number> findIndexValues(Object self, Closure closure) {
15440 return findIndexValues(self, 0, closure);
15441 }
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454 public static List<Number> findIndexValues(Object self, Number startIndex, Closure closure) {
15455 List<Number> result = new ArrayList<Number>();
15456 long count = 0;
15457 long startCount = startIndex.longValue();
15458 BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
15459 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); count++) {
15460 Object value = iter.next();
15461 if (count < startCount) {
15462 continue;
15463 }
15464 if (bcw.call(value)) {
15465 result.add(count);
15466 }
15467 }
15468 return result;
15469 }
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483 public static ClassLoader getRootLoader(ClassLoader self) {
15484 while (true) {
15485 if (self == null) return null;
15486 if (isRootLoaderClassOrSubClass(self)) return self;
15487 self = self.getParent();
15488 }
15489 }
15490
15491 private static boolean isRootLoaderClassOrSubClass(ClassLoader self) {
15492 Class current = self.getClass();
15493 while(!current.getName().equals(Object.class.getName())) {
15494 if(current.getName().equals(RootLoader.class.getName())) return true;
15495 current = current.getSuperclass();
15496 }
15497
15498 return false;
15499 }
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511 @SuppressWarnings("unchecked")
15512 public static <T> T asType(Object obj, Class<T> type) {
15513 if (String.class == type) {
15514 return (T) InvokerHelper.toString(obj);
15515 }
15516
15517
15518 try {
15519 return (T) DefaultTypeTransformation.castToType(obj, type);
15520 }
15521 catch (GroovyCastException e) {
15522 MetaClass mc = InvokerHelper.getMetaClass(obj);
15523 if (mc instanceof ExpandoMetaClass) {
15524 ExpandoMetaClass emc = (ExpandoMetaClass) mc;
15525 Object mixedIn = emc.castToMixedType(obj, type);
15526 if (mixedIn != null)
15527 return (T) mixedIn;
15528 }
15529 if (type.isInterface()) {
15530 try {
15531 List<Class> interfaces = new ArrayList<Class>();
15532 interfaces.add(type);
15533 return (T) ProxyGenerator.INSTANCE.instantiateDelegate(interfaces, obj);
15534 } catch (GroovyRuntimeException cause) {
15535
15536 }
15537 }
15538 throw e;
15539 }
15540 }
15541
15542 private static Object asArrayType(Object object, Class type) {
15543 if (type.isAssignableFrom(object.getClass())) {
15544 return object;
15545 }
15546 Collection list = DefaultTypeTransformation.asCollection(object);
15547 int size = list.size();
15548 Class elementType = type.getComponentType();
15549 Object array = Array.newInstance(elementType, size);
15550 int idx = 0;
15551
15552 if (boolean.class.equals(elementType)) {
15553 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15554 Object element = iter.next();
15555 Array.setBoolean(array, idx, (Boolean) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, boolean.class}));
15556 }
15557 } else if (byte.class.equals(elementType)) {
15558 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15559 Object element = iter.next();
15560 Array.setByte(array, idx, (Byte) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, byte.class}));
15561 }
15562 } else if (char.class.equals(elementType)) {
15563 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15564 Object element = iter.next();
15565 Array.setChar(array, idx, (Character) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, char.class}));
15566 }
15567 } else if (double.class.equals(elementType)) {
15568 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15569 Object element = iter.next();
15570 Array.setDouble(array, idx, (Double) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, double.class}));
15571 }
15572 } else if (float.class.equals(elementType)) {
15573 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15574 Object element = iter.next();
15575 Array.setFloat(array, idx, (Float) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, float.class}));
15576 }
15577 } else if (int.class.equals(elementType)) {
15578 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15579 Object element = iter.next();
15580 Array.setInt(array, idx, (Integer) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, int.class}));
15581 }
15582 } else if (long.class.equals(elementType)) {
15583 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15584 Object element = iter.next();
15585 Array.setLong(array, idx, (Long) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, long.class}));
15586 }
15587 } else if (short.class.equals(elementType)) {
15588 for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15589 Object element = iter.next();
15590 Array.setShort(array, idx, (Short) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, short.class}));
15591 }
15592 } else for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {
15593 Object element = iter.next();
15594 Array.set(array, idx, InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, elementType}));
15595 }
15596 return array;
15597 }
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607 @SuppressWarnings("unchecked")
15608 public static <T> T newInstance(Class<T> c) {
15609 return (T) InvokerHelper.invokeConstructorOf(c, null);
15610 }
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623 @SuppressWarnings("unchecked")
15624 public static <T> T newInstance(Class<T> c, Object[] args) {
15625 if (args == null) args = new Object[]{null};
15626 return (T) InvokerHelper.invokeConstructorOf(c, args);
15627 }
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637 public static MetaClass getMetaClass(Class c) {
15638 MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
15639 MetaClass mc = metaClassRegistry.getMetaClass(c);
15640 if (mc instanceof ExpandoMetaClass
15641 || mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee() instanceof ExpandoMetaClass)
15642 return mc;
15643 else {
15644 return new HandleMetaClass(mc);
15645 }
15646 }
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656 public static MetaClass getMetaClass(Object obj) {
15657 MetaClass mc = InvokerHelper.getMetaClass(obj);
15658 return new HandleMetaClass(mc, obj);
15659 }
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669 public static MetaClass getMetaClass(GroovyObject obj) {
15670
15671 return getMetaClass((Object)obj);
15672 }
15673
15674
15675
15676
15677
15678
15679
15680
15681 public static void setMetaClass(Class self, MetaClass metaClass) {
15682 final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
15683 if (metaClass == null)
15684 metaClassRegistry.removeMetaClass(self);
15685 else {
15686 if (metaClass instanceof HandleMetaClass) {
15687 metaClassRegistry.setMetaClass(self, ((HandleMetaClass)metaClass).getAdaptee());
15688 } else {
15689 metaClassRegistry.setMetaClass(self, metaClass);
15690 }
15691 if (self==NullObject.class) {
15692 NullObject.getNullObject().setMetaClass(metaClass);
15693 }
15694 }
15695 }
15696
15697
15698
15699
15700
15701
15702
15703 public static void setMetaClass(Object self, MetaClass metaClass) {
15704 if (metaClass instanceof HandleMetaClass)
15705 metaClass = ((HandleMetaClass)metaClass).getAdaptee();
15706
15707 if (self instanceof Class) {
15708 GroovySystem.getMetaClassRegistry().setMetaClass((Class) self, metaClass);
15709 } else {
15710 ((MetaClassRegistryImpl)GroovySystem.getMetaClassRegistry()).setMetaClass(self, metaClass);
15711 }
15712 }
15713
15714
15715
15716
15717
15718
15719
15720 public static void setMetaClass(GroovyObject self, MetaClass metaClass) {
15721
15722 if (metaClass instanceof HandleMetaClass)
15723 metaClass = ((HandleMetaClass)metaClass).getAdaptee();
15724
15725 self.setMetaClass(metaClass);
15726 disablePrimitiveOptimization(self);
15727 }
15728
15729 private static void disablePrimitiveOptimization(Object self) {
15730 Field sdyn;
15731 Class c = self.getClass();
15732 try {
15733 sdyn = c.getDeclaredField(Verifier.STATIC_METACLASS_BOOL);
15734 sdyn.setBoolean(null, true);
15735 } catch (Throwable e) {
15736
15737 }
15738 }
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749 public static MetaClass metaClass (Class self, Closure closure){
15750 MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
15751 MetaClass mc = metaClassRegistry.getMetaClass(self);
15752
15753 if (mc instanceof ExpandoMetaClass) {
15754 ((ExpandoMetaClass) mc).define(closure);
15755 return mc;
15756 }
15757 else {
15758 if (mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee() instanceof ExpandoMetaClass) {
15759 ((ExpandoMetaClass)((DelegatingMetaClass) mc).getAdaptee()).define(closure);
15760 return mc;
15761 }
15762 else {
15763 if (mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee().getClass() == MetaClassImpl.class) {
15764 ExpandoMetaClass emc = new ExpandoMetaClass(self, false, true);
15765 emc.initialize();
15766 emc.define(closure);
15767 ((DelegatingMetaClass) mc).setAdaptee(emc);
15768 return mc;
15769 }
15770 else {
15771 if (mc.getClass() == MetaClassImpl.class) {
15772
15773 mc = new ExpandoMetaClass(self, false, true);
15774 mc.initialize();
15775 ((ExpandoMetaClass)mc).define(closure);
15776 metaClassRegistry.setMetaClass(self, mc);
15777 return mc;
15778 }
15779 else {
15780 throw new GroovyRuntimeException("Can't add methods to custom meta class " + mc);
15781 }
15782 }
15783 }
15784 }
15785 }
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796 public static MetaClass metaClass (Object self, Closure closure){
15797 MetaClass emc = hasPerInstanceMetaClass(self);
15798 if (emc == null) {
15799 final ExpandoMetaClass metaClass = new ExpandoMetaClass(self.getClass(), false, true);
15800 metaClass.initialize();
15801 metaClass.define(closure);
15802 if (self instanceof GroovyObject) {
15803 setMetaClass((GroovyObject)self, metaClass);
15804 } else {
15805 setMetaClass(self, metaClass);
15806 }
15807 return metaClass;
15808 }
15809 else {
15810 if (emc instanceof ExpandoMetaClass) {
15811 ((ExpandoMetaClass)emc).define(closure);
15812 return emc;
15813 }
15814 else {
15815 if (emc instanceof DelegatingMetaClass && ((DelegatingMetaClass)emc).getAdaptee() instanceof ExpandoMetaClass) {
15816 ((ExpandoMetaClass)((DelegatingMetaClass)emc).getAdaptee()).define(closure);
15817 return emc;
15818 }
15819 else {
15820 throw new RuntimeException("Can't add methods to non-ExpandoMetaClass " + emc);
15821 }
15822 }
15823 }
15824 }
15825
15826 private static MetaClass hasPerInstanceMetaClass(Object object) {
15827 if (object instanceof GroovyObject) {
15828 MetaClass mc = ((GroovyObject)object).getMetaClass();
15829 if (mc == GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass()) || mc.getClass() == MetaClassImpl.class)
15830 return null;
15831 else
15832 return mc;
15833 }
15834 else {
15835 ClassInfo info = ClassInfo.getClassInfo(object.getClass());
15836 info.lock();
15837 try {
15838 return info.getPerInstanceMetaClass(object);
15839 }
15840 finally {
15841 info.unlock();
15842 }
15843 }
15844 }
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855 public static <T> Iterator<T> iterator(T[] a) {
15856 return DefaultTypeTransformation.asCollection(a).iterator();
15857 }
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868 public static Iterator iterator(Object o) {
15869 return DefaultTypeTransformation.asCollection(o).iterator();
15870 }
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881 public static <T> Iterator<T> iterator(final Enumeration<T> enumeration) {
15882 return new Iterator<T>() {
15883 private T last;
15884
15885 public boolean hasNext() {
15886 return enumeration.hasMoreElements();
15887 }
15888
15889 public T next() {
15890 last = enumeration.nextElement();
15891 return last;
15892 }
15893
15894 public void remove() {
15895 throw new UnsupportedOperationException("Cannot remove() from an Enumeration");
15896 }
15897 };
15898 }
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908 public static <T> Iterator<T> iterator(Iterator<T> self) {
15909 return self;
15910 }
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928 public static List<MetaMethod> respondsTo(Object self, String name, Object[] argTypes) {
15929 return InvokerHelper.getMetaClass(self).respondsTo(self, name, argTypes);
15930 }
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947 public static List<MetaMethod> respondsTo(Object self, String name) {
15948 return InvokerHelper.getMetaClass(self).respondsTo(self, name);
15949 }
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963 public static MetaProperty hasProperty(Object self, String name) {
15964 return InvokerHelper.getMetaClass(self).hasProperty(self, name);
15965 }
15966
15967 @Deprecated
15968 public static boolean asBoolean(CharSequence string) {
15969 return StringGroovyMethods.asBoolean(string);
15970 }
15971
15972 @Deprecated
15973 public static boolean asBoolean(Matcher matcher) {
15974 return StringGroovyMethods.asBoolean(matcher);
15975 }
15976
15977 @Deprecated
15978 public static <T> T asType(CharSequence self, Class<T> c) {
15979 return StringGroovyMethods.asType(self, c);
15980 }
15981
15982 @Deprecated
15983 @SuppressWarnings("unchecked")
15984 public static <T> T asType(GString self, Class<T> c) {
15985 return StringGroovyMethods.asType(self, c);
15986 }
15987
15988 @Deprecated
15989 @SuppressWarnings("unchecked")
15990 public static <T> T asType(String self, Class<T> c) {
15991 return StringGroovyMethods.asType(self, c);
15992 }
15993
15994 @Deprecated
15995 public static Pattern bitwiseNegate(CharSequence self) {
15996 return StringGroovyMethods.bitwiseNegate(self);
15997 }
15998
15999 @Deprecated
16000 public static Pattern bitwiseNegate(String self) {
16001 return StringGroovyMethods.bitwiseNegate(self);
16002 }
16003
16004 @Deprecated
16005 public static CharSequence capitalize(CharSequence self) {
16006 return StringGroovyMethods.capitalize(self);
16007 }
16008
16009 @Deprecated
16010 public static String capitalize(String self) {
16011 return StringGroovyMethods.capitalize(self);
16012 }
16013
16014 @Deprecated
16015 public static CharSequence center(CharSequence self, Number numberOfChars) {
16016 return StringGroovyMethods.center(self, numberOfChars);
16017 }
16018
16019 @Deprecated
16020 public static CharSequence center(CharSequence self, Number numberOfChars, CharSequence padding) {
16021 return StringGroovyMethods.center(self, numberOfChars, padding);
16022 }
16023
16024 @Deprecated
16025 public static String center(String self, Number numberOfChars) {
16026 return StringGroovyMethods.center(self, numberOfChars);
16027 }
16028
16029 @Deprecated
16030 public static String center(String self, Number numberOfChars, String padding) {
16031 return StringGroovyMethods.center(self, numberOfChars, padding);
16032 }
16033
16034 @Deprecated
16035 public static boolean contains(CharSequence self, CharSequence text) {
16036 return StringGroovyMethods.contains(self, text);
16037 }
16038
16039 @Deprecated
16040 public static boolean contains(String self, String text) {
16041 return StringGroovyMethods.contains(self, text);
16042 }
16043
16044 @Deprecated
16045 public static int count(CharSequence self, CharSequence text) {
16046 return StringGroovyMethods.count(self, text);
16047 }
16048
16049 @Deprecated
16050 public static int count(String self, String text) {
16051 return StringGroovyMethods.count(self, text);
16052 }
16053
16054 @Deprecated
16055 protected static StringBufferWriter createStringBufferWriter(StringBuffer self) {
16056 return new StringBufferWriter(self);
16057 }
16058
16059 @Deprecated
16060 protected static StringWriter createStringWriter(String self) {
16061 StringWriter answer = new StringWriter();
16062 answer.write(self);
16063 return answer;
16064 }
16065
16066 @Deprecated
16067 public static CharSequence denormalize(final CharSequence self) {
16068 return StringGroovyMethods.denormalize(self);
16069 }
16070
16071 @Deprecated
16072 public static String denormalize(final String self) {
16073 return StringGroovyMethods.denormalize(self);
16074 }
16075
16076 @Deprecated
16077 public static CharSequence
16078 drop(CharSequence self, int num) {
16079 return StringGroovyMethods.drop(self, num);
16080 }
16081
16082 @Deprecated
16083 public static <T> T eachLine(CharSequence self, Closure<T> closure) throws IOException {
16084 return StringGroovyMethods.eachLine(self, closure);
16085 }
16086
16087 @Deprecated
16088 public static <T> T eachLine(CharSequence self, int firstLine, Closure<T> closure) throws IOException {
16089 return StringGroovyMethods.eachLine(self, firstLine, closure);
16090 }
16091
16092 @Deprecated
16093 public static <T> T eachLine(String self, Closure<T> closure) throws IOException {
16094 return StringGroovyMethods.eachLine(self, closure);
16095 }
16096
16097 @Deprecated
16098 public static <T> T eachLine(String self, int firstLine, Closure<T> closure) throws IOException {
16099 return StringGroovyMethods.eachLine(self, firstLine, closure);
16100 }
16101
16102 @Deprecated
16103 public static String eachMatch(CharSequence self, CharSequence regex, Closure closure) {
16104 return (String) StringGroovyMethods.eachMatch(self, regex, closure);
16105 }
16106
16107 @Deprecated
16108 public static String eachMatch(CharSequence self, Pattern pattern, Closure closure) {
16109 return (String) StringGroovyMethods.eachMatch(self, pattern, closure);
16110 }
16111
16112 @Deprecated
16113 public static String eachMatch(String self, Pattern pattern, Closure closure) {
16114 return StringGroovyMethods.eachMatch(self, pattern, closure);
16115 }
16116
16117 @Deprecated
16118 public static String eachMatch(String self, String regex, Closure closure) {
16119 return StringGroovyMethods.eachMatch(self, regex, closure);
16120 }
16121
16122 @Deprecated
16123 public static CharSequence expand(CharSequence self) {
16124 return StringGroovyMethods.expand(self);
16125 }
16126
16127 @Deprecated
16128 public static CharSequence expand(CharSequence self, int tabStop) {
16129 return StringGroovyMethods.expand(self, tabStop);
16130 }
16131
16132 @Deprecated
16133 public static String expand(String self) {
16134 return StringGroovyMethods.expand(self);
16135 }
16136
16137 @Deprecated
16138 public static String expand(String self, int tabStop) {
16139 return StringGroovyMethods.expand(self, tabStop);
16140 }
16141
16142 @Deprecated
16143 public static CharSequence expandLine(CharSequence self, int tabStop) {
16144 return StringGroovyMethods.expandLine(self, tabStop);
16145 }
16146
16147 @Deprecated
16148 public static String expandLine(String self, int tabStop) {
16149 return StringGroovyMethods.expandLine(self, tabStop);
16150 }
16151
16152 @Deprecated
16153 public static CharSequence find(CharSequence self, CharSequence regex) {
16154 return StringGroovyMethods.find(self, regex);
16155 }
16156
16157 @Deprecated
16158 public static CharSequence find(CharSequence self, CharSequence regex, Closure closure) {
16159 return StringGroovyMethods.find(self, regex, closure);
16160 }
16161
16162 @Deprecated
16163 public static CharSequence find(CharSequence self, Pattern pattern) {
16164 return StringGroovyMethods.find(self, pattern);
16165 }
16166
16167 @Deprecated
16168 public static CharSequence find(CharSequence self, Pattern pattern, Closure closure) {
16169 return StringGroovyMethods.find(self, pattern, closure);
16170 }
16171
16172 @Deprecated
16173 public static String find(String self, Pattern pattern) {
16174 return StringGroovyMethods.find(self, pattern);
16175 }
16176
16177 @Deprecated
16178 public static String find(String self, Pattern pattern, Closure closure) {
16179 return StringGroovyMethods.find(self, pattern, closure);
16180 }
16181
16182 @Deprecated
16183 public static String find(String self, String regex) {
16184 return StringGroovyMethods.find(self, regex);
16185 }
16186
16187 @Deprecated
16188 public static String find(String self, String regex, Closure closure) {
16189 return StringGroovyMethods.find(self, regex, closure);
16190 }
16191
16192 @Deprecated
16193 public static List<String> findAll(CharSequence self, CharSequence regex) {
16194 return StringGroovyMethods.findAll(self, regex);
16195 }
16196
16197 @Deprecated
16198 public static <T> List<T> findAll(CharSequence self, CharSequence regex, Closure<T> closure) {
16199 return StringGroovyMethods.findAll(self, regex, closure);
16200 }
16201
16202 @Deprecated
16203 public static List<String> findAll(CharSequence self, Pattern pattern) {
16204 return StringGroovyMethods.findAll(self, pattern);
16205 }
16206
16207 @Deprecated
16208 public static <T> List<T> findAll(CharSequence self, Pattern pattern, Closure<T> closure) {
16209 return StringGroovyMethods.findAll(self, pattern, closure);
16210 }
16211
16212 @Deprecated
16213 public static List<String> findAll(String self, Pattern pattern) {
16214 return StringGroovyMethods.findAll(self, pattern);
16215 }
16216
16217 @Deprecated
16218 public static <T> List<T> findAll(String self, Pattern pattern, Closure<T> closure) {
16219 return StringGroovyMethods.findAll(self, pattern, closure);
16220 }
16221
16222 @Deprecated
16223 public static List<String> findAll(String self, String regex) {
16224 return StringGroovyMethods.findAll(self, regex);
16225 }
16226
16227 @Deprecated
16228 public static <T> List<T> findAll(String self, String regex, Closure<T> closure) {
16229 return StringGroovyMethods.findAll(self, regex, closure);
16230 }
16231
16232 @Deprecated
16233 public static CharSequence getAt(CharSequence self, Collection indices) {
16234 return StringGroovyMethods.getAt(self, indices);
16235 }
16236
16237 @Deprecated
16238 public static CharSequence getAt(CharSequence text, EmptyRange range) {
16239 return StringGroovyMethods.getAt(text, range);
16240 }
16241
16242 @Deprecated
16243 public static CharSequence getAt(CharSequence text, int index) {
16244 return StringGroovyMethods.getAt(text, index);
16245 }
16246
16247 @Deprecated
16248 public static CharSequence getAt(CharSequence text, IntRange range) {
16249 return StringGroovyMethods.getAt(text, range);
16250 }
16251
16252 @Deprecated
16253 public static CharSequence getAt(CharSequence text, Range range) {
16254 return StringGroovyMethods.getAt(text, range);
16255 }
16256
16257 @Deprecated
16258 public static List getAt(Matcher self, Collection indices) {
16259 return StringGroovyMethods.getAt(self, indices);
16260 }
16261
16262 @Deprecated
16263 public static Object getAt(Matcher matcher, int idx) {
16264 return StringGroovyMethods.getAt(matcher, idx);
16265 }
16266
16267 @Deprecated
16268 public static String getAt(String self, Collection indices) {
16269 return StringGroovyMethods.getAt(self, indices);
16270 }
16271
16272 @Deprecated
16273 public static String getAt(String text, EmptyRange range) {
16274 return StringGroovyMethods.getAt(text, range);
16275 }
16276
16277 @Deprecated
16278 public static String getAt(String text, int index) {
16279 return StringGroovyMethods.getAt(text, index);
16280 }
16281
16282 @Deprecated
16283 public static String getAt(String text, IntRange range) {
16284 return StringGroovyMethods.getAt(text, range);
16285 }
16286
16287 @Deprecated
16288 public static String getAt(String text, Range range) {
16289 return StringGroovyMethods.getAt(text, range);
16290 }
16291
16292 @Deprecated
16293 public static char[] getChars(CharSequence self) {
16294 return StringGroovyMethods.getChars(self);
16295 }
16296
16297 @Deprecated
16298 public static char[] getChars(String self) {
16299 return StringGroovyMethods.getChars(self);
16300 }
16301
16302 @Deprecated
16303 public static int getCount(Matcher matcher) {
16304 return StringGroovyMethods.getCount(matcher);
16305 }
16306
16307 @Deprecated
16308 public static boolean hasGroup(Matcher matcher) {
16309 return StringGroovyMethods.hasGroup(matcher);
16310 }
16311
16312 @Deprecated
16313 public static boolean isAllWhitespace(CharSequence self) {
16314 return StringGroovyMethods.isAllWhitespace(self);
16315 }
16316
16317 @Deprecated
16318 public static boolean isAllWhitespace(String self) {
16319 return StringGroovyMethods.isAllWhitespace(self);
16320 }
16321
16322 @Deprecated
16323 public static boolean isBigDecimal(CharSequence self) {
16324 return StringGroovyMethods.isBigDecimal(self);
16325 }
16326
16327 @Deprecated
16328 public static boolean isBigDecimal(String self) {
16329 return StringGroovyMethods.isBigDecimal(self);
16330 }
16331
16332 @Deprecated
16333 public static boolean isBigInteger(CharSequence self) {
16334 return StringGroovyMethods.isBigInteger(self);
16335 }
16336
16337 @Deprecated
16338 public static boolean isBigInteger(String self) {
16339 return StringGroovyMethods.isBigInteger(self);
16340 }
16341
16342 @Deprecated
16343 public static boolean isCase(CharSequence caseValue, Object switchValue) {
16344 return StringGroovyMethods.isCase(caseValue, switchValue);
16345 }
16346
16347 @Deprecated
16348 public static boolean isCase(GString caseValue, Object switchValue) {
16349 return StringGroovyMethods.isCase(caseValue, switchValue);
16350 }
16351
16352 @Deprecated
16353 public static boolean isCase(Pattern caseValue, Object switchValue) {
16354 return StringGroovyMethods.isCase(caseValue, switchValue);
16355 }
16356
16357 @Deprecated
16358 public static boolean isCase(String caseValue, Object switchValue) {
16359 return StringGroovyMethods.isCase(caseValue, switchValue);
16360 }
16361
16362 @Deprecated
16363 public static boolean isDouble(CharSequence self) {
16364 return StringGroovyMethods.isDouble(self);
16365 }
16366
16367 @Deprecated
16368 public static boolean isDouble(String self) {
16369 return StringGroovyMethods.isDouble(self);
16370 }
16371
16372 @Deprecated
16373 public static boolean isFloat(CharSequence self) {
16374 return StringGroovyMethods.isFloat(self);
16375 }
16376
16377 @Deprecated
16378 public static boolean isFloat(String self) {
16379 return StringGroovyMethods.isFloat(self);
16380 }
16381
16382 @Deprecated
16383 public static boolean isInteger(CharSequence self) {
16384 return StringGroovyMethods.isInteger(self);
16385 }
16386
16387 @Deprecated
16388 public static boolean isInteger(String self) {
16389 return StringGroovyMethods.isInteger(self);
16390 }
16391
16392 @Deprecated
16393 public static boolean isLong(CharSequence self) {
16394 return StringGroovyMethods.isLong(self);
16395 }
16396
16397 @Deprecated
16398 public static boolean isLong(String self) {
16399 return StringGroovyMethods.isLong(self);
16400 }
16401
16402 @Deprecated
16403 public static boolean isNumber(CharSequence self) {
16404 return StringGroovyMethods.isNumber(self);
16405 }
16406
16407 @Deprecated
16408 public static boolean isNumber(String self) {
16409 return StringGroovyMethods.isNumber(self);
16410 }
16411
16412 @Deprecated
16413 public static Iterator iterator(final Matcher matcher) {
16414 return StringGroovyMethods.iterator(matcher);
16415 }
16416
16417 @Deprecated
16418 public static StringBuilder leftShift(CharSequence self, Object value) {
16419 return StringGroovyMethods.leftShift(self, value);
16420 }
16421
16422 @Deprecated
16423 public static StringBuffer leftShift(String self, Object value) {
16424 return StringGroovyMethods.leftShift(self, value);
16425 }
16426
16427 @Deprecated
16428 public static StringBuffer leftShift(StringBuffer self, Object value) {
16429 return StringGroovyMethods.leftShift(self, value);
16430 }
16431
16432 @Deprecated
16433 public static StringBuilder leftShift(StringBuilder self, Object value) {
16434 return StringGroovyMethods.leftShift(self, value);
16435 }
16436
16437 @Deprecated
16438 public static boolean matches(CharSequence self, Pattern pattern) {
16439 return StringGroovyMethods.matches(self, pattern);
16440 }
16441
16442 @Deprecated
16443 public static boolean matches(String self, Pattern pattern) {
16444 return StringGroovyMethods.matches(self, pattern);
16445 }
16446
16447 @Deprecated
16448 public static CharSequence minus(CharSequence self, Object target) {
16449 return StringGroovyMethods.minus(self, target);
16450 }
16451
16452 @Deprecated
16453 public static String minus(String self, Object target) {
16454 return StringGroovyMethods.minus(self, target);
16455 }
16456
16457 @Deprecated
16458 public static CharSequence multiply(CharSequence self, Number factor) {
16459 return StringGroovyMethods.multiply(self, factor);
16460 }
16461
16462 @Deprecated
16463 public static String multiply(String self, Number factor) {
16464 return StringGroovyMethods.multiply(self, factor);
16465 }
16466
16467 @Deprecated
16468 public static CharSequence next(CharSequence self) {
16469 return StringGroovyMethods.next(self);
16470 }
16471
16472 @Deprecated
16473 public static String next(String self) {
16474 return StringGroovyMethods.next(self);
16475 }
16476
16477 @Deprecated
16478 public static CharSequence normalize(final CharSequence self) {
16479 return StringGroovyMethods.normalize(self);
16480 }
16481
16482 @Deprecated
16483 public static String normalize(final String self) {
16484 return StringGroovyMethods.normalize(self);
16485 }
16486
16487 @Deprecated
16488 public static CharSequence padLeft(CharSequence self, Number numberOfChars) {
16489 return StringGroovyMethods.padLeft(self, numberOfChars);
16490 }
16491
16492 @Deprecated
16493 public static CharSequence padLeft(CharSequence self, Number numberOfChars, CharSequence padding) {
16494 return StringGroovyMethods.padLeft(self, numberOfChars, padding);
16495 }
16496
16497 @Deprecated
16498 public static String padLeft(String self, Number numberOfChars) {
16499 return StringGroovyMethods.padLeft(self, numberOfChars);
16500 }
16501
16502 @Deprecated
16503 public static String padLeft(String self, Number numberOfChars, String padding) {
16504 return StringGroovyMethods.padLeft(self, numberOfChars, padding);
16505 }
16506
16507 @Deprecated
16508 public static CharSequence padRight(CharSequence self, Number numberOfChars) {
16509 return StringGroovyMethods.padRight(self, numberOfChars);
16510 }
16511
16512 @Deprecated
16513 public static CharSequence padRight(CharSequence self, Number numberOfChars, CharSequence padding) {
16514 return StringGroovyMethods.padRight(self, numberOfChars, padding);
16515 }
16516
16517 @Deprecated
16518 public static String padRight(String self, Number numberOfChars) {
16519 return StringGroovyMethods.padRight(self, numberOfChars);
16520 }
16521
16522 @Deprecated
16523 public static String padRight(String self, Number numberOfChars, String padding) {
16524 return StringGroovyMethods.padRight(self, numberOfChars, padding);
16525 }
16526
16527 @Deprecated
16528 public static CharSequence plus(CharSequence left, Object value) {
16529 return StringGroovyMethods.plus(left, value);
16530 }
16531
16532 @Deprecated
16533 public static String plus(Number value, String right) {
16534 return StringGroovyMethods.plus(value, right);
16535 }
16536
16537 @Deprecated
16538 public static String plus(String left, Object value) {
16539 return StringGroovyMethods.plus(left, value);
16540 }
16541
16542 @Deprecated
16543 public static String plus(StringBuffer left, String value) {
16544 return StringGroovyMethods.plus(left, value);
16545 }
16546
16547 @Deprecated
16548 public static CharSequence previous(CharSequence self) {
16549 return StringGroovyMethods.previous(self);
16550 }
16551
16552 @Deprecated
16553 public static String previous(String self) {
16554 return StringGroovyMethods.previous(self);
16555 }
16556
16557 @Deprecated
16558 public static void putAt(StringBuffer self, EmptyRange range, Object value) {
16559 StringGroovyMethods.putAt(self, range, value);
16560 }
16561
16562 @Deprecated
16563 public static void putAt(StringBuffer self, IntRange range, Object value) {
16564 StringGroovyMethods.putAt(self, range, value);
16565 }
16566
16567 @Deprecated
16568 public static List<String> readLines(CharSequence self) throws IOException {
16569 return StringGroovyMethods.readLines(self);
16570 }
16571
16572 @Deprecated
16573 public static List<String> readLines(String self) throws IOException {
16574 return StringGroovyMethods.readLines(self);
16575 }
16576
16577 @Deprecated
16578 public static CharSequence replaceAll(final CharSequence self, final CharSequence regex, final CharSequence replacement) {
16579 return StringGroovyMethods.replaceAll(self, regex, replacement);
16580 }
16581
16582 @Deprecated
16583 public static CharSequence replaceAll(final CharSequence self, final CharSequence regex, final Closure closure) {
16584 return StringGroovyMethods.replaceAll(self, regex, closure);
16585 }
16586
16587 @Deprecated
16588 public static CharSequence replaceAll(CharSequence self, Pattern pattern, CharSequence replacement) {
16589 return StringGroovyMethods.replaceAll(self, pattern, replacement);
16590 }
16591
16592 @Deprecated
16593 public static String replaceAll(final CharSequence self, final Pattern pattern, final Closure closure) {
16594 return StringGroovyMethods.replaceAll(self, pattern, closure);
16595 }
16596
16597 @Deprecated
16598 public static String replaceAll(final String self, final Pattern pattern, final Closure closure) {
16599 return StringGroovyMethods.replaceAll(self, pattern, closure);
16600 }
16601
16602 @Deprecated
16603 public static String replaceAll(String self, Pattern pattern, String replacement) {
16604 return StringGroovyMethods.replaceAll(self, pattern, replacement);
16605 }
16606
16607 @Deprecated
16608 public static String replaceAll(final String self, final String regex, final Closure closure) {
16609 return StringGroovyMethods.replaceAll(self, regex, closure);
16610 }
16611
16612 @Deprecated
16613 public static String replaceFirst(final CharSequence self, final CharSequence regex, final CharSequence replacement) {
16614 return StringGroovyMethods.replaceFirst(self, regex, replacement);
16615 }
16616
16617 @Deprecated
16618 public static String replaceFirst(final CharSequence self, final CharSequence regex, final Closure closure) {
16619 return StringGroovyMethods.replaceFirst(self, regex, closure);
16620 }
16621
16622 @Deprecated
16623 public static CharSequence replaceFirst(CharSequence self, Pattern pattern, CharSequence replacement) {
16624 return StringGroovyMethods.replaceFirst(self, pattern, replacement);
16625 }
16626
16627 @Deprecated
16628 public static String replaceFirst(final CharSequence self, final Pattern pattern, final Closure closure) {
16629 return StringGroovyMethods.replaceFirst(self, pattern, closure);
16630 }
16631
16632 @Deprecated
16633 public static String replaceFirst(final String self, final Pattern pattern, final Closure closure) {
16634 return StringGroovyMethods.replaceFirst(self, pattern, closure);
16635 }
16636
16637 @Deprecated
16638 public static String replaceFirst(String self, Pattern pattern, String replacement) {
16639 return StringGroovyMethods.replaceFirst(self, pattern, replacement);
16640 }
16641
16642 @Deprecated
16643 public static String replaceFirst(final String self, final String regex, final Closure closure) {
16644 return StringGroovyMethods.replaceFirst(self, regex, closure);
16645 }
16646
16647 @Deprecated
16648 public static CharSequence reverse(CharSequence self) {
16649 return StringGroovyMethods.reverse(self);
16650 }
16651
16652 @Deprecated
16653 public static String reverse(String self) {
16654 return StringGroovyMethods.reverse(self);
16655 }
16656
16657 @Deprecated
16658 public static void setIndex(Matcher matcher, int idx) {
16659 StringGroovyMethods.setIndex(matcher, idx);
16660 }
16661
16662 @Deprecated
16663 public static int size(CharSequence text) {
16664 return StringGroovyMethods.size(text);
16665 }
16666
16667 @Deprecated
16668 public static long size(Matcher self) {
16669 return StringGroovyMethods.size(self);
16670 }
16671
16672 @Deprecated
16673 public static int size(String text) {
16674 return StringGroovyMethods.size(text);
16675 }
16676
16677 @Deprecated
16678 public static int size(StringBuffer buffer) {
16679 return StringGroovyMethods.size(buffer);
16680 }
16681
16682 @Deprecated
16683 public static CharSequence[] split(CharSequence self) {
16684 return StringGroovyMethods.split(self);
16685 }
16686
16687 @Deprecated
16688 public static String[] split(GString self) {
16689 return StringGroovyMethods.split(self);
16690 }
16691
16692 @Deprecated
16693 public static String[] split(String self) {
16694 return StringGroovyMethods.split(self);
16695 }
16696
16697 @Deprecated
16698 public static <T> T splitEachLine(CharSequence self, CharSequence regex, Closure<T> closure) throws IOException {
16699 return StringGroovyMethods.splitEachLine(self, regex, closure);
16700 }
16701
16702 @Deprecated
16703 public static <T> T splitEachLine(CharSequence self, Pattern pattern, Closure<T> closure) throws IOException {
16704 return StringGroovyMethods.splitEachLine(self, pattern, closure);
16705 }
16706
16707 @Deprecated
16708 public static <T> T splitEachLine(String self, Pattern pattern, Closure<T> closure) throws IOException {
16709 return StringGroovyMethods.splitEachLine(self, pattern, closure);
16710 }
16711
16712 @Deprecated
16713 public static <T> T splitEachLine(String self, String regex, Closure<T> closure) throws IOException {
16714 return StringGroovyMethods.splitEachLine(self, regex, closure);
16715 }
16716
16717 @Deprecated
16718 public static CharSequence stripIndent(CharSequence self) {
16719 return StringGroovyMethods.stripIndent(self);
16720 }
16721
16722 @Deprecated
16723 public static CharSequence stripIndent(CharSequence self, int numChars) {
16724 return StringGroovyMethods.stripIndent(self, numChars);
16725 }
16726
16727 @Deprecated
16728 public static String stripIndent(String self) {
16729 return StringGroovyMethods.stripIndent(self);
16730 }
16731
16732 @Deprecated
16733 public static String stripIndent(String self, int numChars) {
16734 return StringGroovyMethods.stripIndent(self, numChars);
16735 }
16736
16737 @Deprecated
16738 public static CharSequence stripMargin(CharSequence self) {
16739 return StringGroovyMethods.stripMargin(self);
16740 }
16741
16742 @Deprecated
16743 public static CharSequence stripMargin(CharSequence self, char marginChar) {
16744 return StringGroovyMethods.stripMargin(self, marginChar);
16745 }
16746
16747 @Deprecated
16748 public static String stripMargin(CharSequence self, CharSequence marginChar) {
16749 return StringGroovyMethods.stripMargin(self, marginChar);
16750 }
16751
16752 @Deprecated
16753 public static String stripMargin(String self) {
16754 return StringGroovyMethods.stripMargin(self);
16755 }
16756
16757 @Deprecated
16758 public static String stripMargin(String self, char marginChar) {
16759 return StringGroovyMethods.stripMargin(self, marginChar);
16760 }
16761
16762 @Deprecated
16763 public static String stripMargin(String self, String marginChar) {
16764 return StringGroovyMethods.stripMargin(self, marginChar);
16765 }
16766
16767 @Deprecated
16768 public static BigDecimal toBigDecimal(CharSequence self) {
16769 return StringGroovyMethods.toBigDecimal(self);
16770 }
16771
16772 @Deprecated
16773 public static BigDecimal toBigDecimal(String self) {
16774 return StringGroovyMethods.toBigDecimal(self);
16775 }
16776
16777 @Deprecated
16778 public static BigInteger toBigInteger(CharSequence self) {
16779 return StringGroovyMethods.toBigInteger(self);
16780 }
16781
16782 @Deprecated
16783 public static BigInteger toBigInteger(String self) {
16784 return StringGroovyMethods.toBigInteger(self);
16785 }
16786
16787 @Deprecated
16788 public static Boolean toBoolean(String self) {
16789 return StringGroovyMethods.toBoolean(self);
16790 }
16791
16792 @Deprecated
16793 public static Character toCharacter(String self) {
16794 return StringGroovyMethods.toCharacter(self);
16795 }
16796
16797 @Deprecated
16798 public static Double toDouble(CharSequence self) {
16799 return StringGroovyMethods.toDouble(self);
16800 }
16801
16802 @Deprecated
16803 public static Double toDouble(String self) {
16804 return StringGroovyMethods.toDouble(self);
16805 }
16806
16807 @Deprecated
16808 public static Float toFloat(CharSequence self) {
16809 return StringGroovyMethods.toFloat(self);
16810 }
16811
16812 @Deprecated
16813 public static Float toFloat(String self) {
16814 return StringGroovyMethods.toFloat(self);
16815 }
16816
16817 @Deprecated
16818 public static Integer toInteger(CharSequence self) {
16819 return StringGroovyMethods.toInteger(self);
16820 }
16821
16822 @Deprecated
16823 public static Integer toInteger(String self) {
16824 return StringGroovyMethods.toInteger(self);
16825 }
16826
16827 @Deprecated
16828 public static List<String> tokenize(CharSequence self) {
16829 return StringGroovyMethods.tokenize(self);
16830 }
16831
16832 @Deprecated
16833 public static List<String> tokenize(CharSequence self, Character token) {
16834 return StringGroovyMethods.tokenize(self, token);
16835 }
16836
16837 @Deprecated
16838 public static List<String> tokenize(CharSequence self, CharSequence token) {
16839 return StringGroovyMethods.tokenize(self, token);
16840 }
16841
16842 @Deprecated
16843 @SuppressWarnings("unchecked")
16844 public static List<String> tokenize(String self) {
16845 return StringGroovyMethods.tokenize(self);
16846 }
16847
16848 @Deprecated
16849 public static List<String> tokenize(String self, Character token) {
16850 return StringGroovyMethods.tokenize(self, token);
16851 }
16852
16853 @Deprecated
16854 @SuppressWarnings("unchecked")
16855 public static List<String> tokenize(String self, String token) {
16856 return StringGroovyMethods.tokenize(self, token);
16857 }
16858
16859 @Deprecated
16860 public static List<String> toList(CharSequence self) {
16861 return StringGroovyMethods.toList(self);
16862 }
16863
16864 @Deprecated
16865 public static List<String> toList(String self) {
16866 return StringGroovyMethods.toList(self);
16867 }
16868
16869 @Deprecated
16870 public static Long toLong(CharSequence self) {
16871 return StringGroovyMethods.toLong(self);
16872 }
16873
16874 @Deprecated
16875 public static Long toLong(String self) {
16876 return StringGroovyMethods.toLong(self);
16877 }
16878
16879 @Deprecated
16880 public static Set<String> toSet(CharSequence self) {
16881 return StringGroovyMethods.toSet(self);
16882 }
16883
16884 @Deprecated
16885 public static Set<String> toSet(String self) {
16886 return StringGroovyMethods.toSet(self);
16887 }
16888
16889 @Deprecated
16890 public static Short toShort(CharSequence self) {
16891 return StringGroovyMethods.toShort(self);
16892 }
16893
16894 @Deprecated
16895 public static Short toShort(String self) {
16896 return StringGroovyMethods.toShort(self);
16897 }
16898
16899 @Deprecated
16900 public static URI toURI(CharSequence self) throws URISyntaxException {
16901 return ResourceGroovyMethods.toURI(self);
16902 }
16903
16904 @Deprecated
16905 public static URI toURI(String self) throws URISyntaxException {
16906 return ResourceGroovyMethods.toURI(self);
16907 }
16908
16909 @Deprecated
16910 public static URL toURL(CharSequence self) throws MalformedURLException {
16911 return ResourceGroovyMethods.toURL(self);
16912 }
16913
16914 @Deprecated
16915 public static URL toURL(String self) throws MalformedURLException {
16916 return ResourceGroovyMethods.toURL(self);
16917 }
16918
16919 @Deprecated
16920 public static CharSequence tr(final CharSequence self, CharSequence sourceSet, CharSequence replacementSet) throws ClassNotFoundException {
16921 return StringGroovyMethods.tr(self, sourceSet, replacementSet);
16922 }
16923
16924 @Deprecated
16925 public static String tr(final String self, String sourceSet, String replacementSet) throws ClassNotFoundException {
16926 return StringGroovyMethods.tr(self, sourceSet, replacementSet);
16927 }
16928
16929 @Deprecated
16930 public static CharSequence unexpand(CharSequence self) {
16931 return StringGroovyMethods.unexpand(self);
16932 }
16933
16934 @Deprecated
16935 public static CharSequence unexpand(CharSequence self, int tabStop) {
16936 return StringGroovyMethods.unexpand(self, tabStop);
16937 }
16938
16939 @Deprecated
16940 public static String unexpand(String self) {
16941 return StringGroovyMethods.unexpand(self);
16942 }
16943
16944 @Deprecated
16945 public static String unexpand(String self, int tabStop) {
16946 return StringGroovyMethods.unexpand(self, tabStop);
16947 }
16948
16949 @Deprecated
16950 public static CharSequence unexpandLine(CharSequence self, int tabStop) {
16951 return StringGroovyMethods.unexpandLine(self, tabStop);
16952 }
16953
16954 @Deprecated
16955 public static String unexpandLine(String self, int tabStop) {
16956 return StringGroovyMethods.unexpandLine(self, tabStop);
16957 }
16958
16959 @Deprecated
16960 public static Process execute(final String self) throws IOException {
16961 return ProcessGroovyMethods.execute(self);
16962 }
16963
16964 @Deprecated
16965 public static Process execute(final String self, final String[] envp, final File dir) throws IOException {
16966 return ProcessGroovyMethods.execute(self, envp, dir);
16967 }
16968
16969 @Deprecated
16970 public static Process execute(final String self, final List envp, final File dir) throws IOException {
16971 return ProcessGroovyMethods.execute(self, envp, dir);
16972 }
16973
16974 @Deprecated
16975 public static Process execute(final String[] commandArray) throws IOException {
16976 return ProcessGroovyMethods.execute(commandArray);
16977 }
16978
16979 @Deprecated
16980 public static Process execute(final String[] commandArray, final String[] envp, final File dir) throws IOException {
16981 return ProcessGroovyMethods.execute(commandArray, envp, dir);
16982 }
16983
16984 @Deprecated
16985 public static Process execute(final String[] commandArray, final List envp, final File dir) throws IOException {
16986 return ProcessGroovyMethods.execute(commandArray, envp, dir);
16987 }
16988
16989 @Deprecated
16990 public static Process execute(final List commands) throws IOException {
16991 return ProcessGroovyMethods.execute(commands);
16992 }
16993
16994 @Deprecated
16995 public static Process execute(final List commands, final String[] envp, final File dir) throws IOException {
16996 return ProcessGroovyMethods.execute(commands, envp, dir);
16997 }
16998
16999 @Deprecated
17000 public static Process execute(final List commands, final List envp, final File dir) throws IOException {
17001 return ProcessGroovyMethods.execute(commands, envp, dir);
17002 }
17003
17004 @Deprecated
17005 public static <T> T withStreams(Socket socket, Closure<T> closure) throws IOException {
17006 return SocketGroovyMethods.withStreams(socket, closure);
17007 }
17008
17009 @Deprecated
17010 public static <T> T withObjectStreams(Socket socket, Closure<T> closure) throws IOException {
17011 return SocketGroovyMethods.withObjectStreams(socket, closure);
17012 }
17013
17014 @Deprecated
17015 public static Writer leftShift(Socket self, Object value) throws IOException {
17016 return SocketGroovyMethods.leftShift(self, value);
17017 }
17018
17019 @Deprecated
17020 public static OutputStream leftShift(Socket self, byte[] value) throws IOException {
17021 return SocketGroovyMethods.leftShift(self, value);
17022 }
17023
17024 @Deprecated
17025 public static Socket accept(ServerSocket serverSocket, final Closure closure) throws IOException {
17026 return SocketGroovyMethods.accept(serverSocket, closure);
17027 }
17028
17029 @Deprecated
17030 public static Socket accept(ServerSocket serverSocket, final boolean runInANewThread,
17031 final Closure closure) throws IOException {
17032 return SocketGroovyMethods.accept(serverSocket, runInANewThread, closure);
17033 }
17034
17035 @Deprecated
17036 public static long size(File self) {
17037 return ResourceGroovyMethods.size(self);
17038 }
17039
17040 @Deprecated
17041 public static Writer leftShift(Writer self, Object value) throws IOException {
17042 return IOGroovyMethods.leftShift(self, value);
17043 }
17044
17045 @Deprecated
17046 public static void write(Writer self, Writable writable) throws IOException {
17047 IOGroovyMethods.write(self, writable);
17048 }
17049
17050 @Deprecated
17051 public static Writer leftShift(OutputStream self, Object value) throws IOException {
17052 return IOGroovyMethods.leftShift(self, value);
17053 }
17054
17055 @Deprecated
17056 public static void leftShift(ObjectOutputStream self, Object value) throws IOException {
17057 IOGroovyMethods.leftShift(self, value);
17058 }
17059
17060 @Deprecated
17061 public static OutputStream leftShift(OutputStream self, InputStream in) throws IOException {
17062 return IOGroovyMethods.leftShift(self, in);
17063 }
17064
17065 @Deprecated
17066 public static OutputStream leftShift(OutputStream self, byte[] value) throws IOException {
17067 return IOGroovyMethods.leftShift(self, value);
17068 }
17069
17070 @Deprecated
17071 public static ObjectOutputStream newObjectOutputStream(File file) throws IOException {
17072 return ResourceGroovyMethods.newObjectOutputStream(file);
17073 }
17074
17075 @Deprecated
17076 public static ObjectOutputStream newObjectOutputStream(OutputStream outputStream) throws IOException {
17077 return IOGroovyMethods.newObjectOutputStream(outputStream);
17078 }
17079
17080 @Deprecated
17081 public static <T> T withObjectOutputStream(File file, Closure<T> closure) throws IOException {
17082 return ResourceGroovyMethods.withObjectOutputStream(file, closure);
17083 }
17084
17085 @Deprecated
17086 public static <T> T withObjectOutputStream(OutputStream outputStream, Closure<T> closure) throws IOException {
17087 return IOGroovyMethods.withObjectOutputStream(outputStream, closure);
17088 }
17089
17090 @Deprecated
17091 public static ObjectInputStream newObjectInputStream(File file) throws IOException {
17092 return ResourceGroovyMethods.newObjectInputStream(file);
17093 }
17094
17095 @Deprecated
17096 public static ObjectInputStream newObjectInputStream(InputStream inputStream) throws IOException {
17097 return IOGroovyMethods.newObjectInputStream(inputStream);
17098 }
17099
17100 @Deprecated
17101 public static ObjectInputStream newObjectInputStream(InputStream inputStream, final ClassLoader classLoader) throws IOException {
17102 return IOGroovyMethods.newObjectInputStream(inputStream, classLoader);
17103 }
17104
17105 @Deprecated
17106 public static ObjectInputStream newObjectInputStream(File file, final ClassLoader classLoader) throws IOException {
17107 return ResourceGroovyMethods.newObjectInputStream(file, classLoader);
17108 }
17109
17110 @Deprecated
17111 public static void eachObject(File self, Closure closure) throws IOException, ClassNotFoundException {
17112 ResourceGroovyMethods.eachObject(self, closure);
17113 }
17114
17115 @Deprecated
17116 public static void eachObject(ObjectInputStream ois, Closure closure) throws IOException, ClassNotFoundException {
17117 IOGroovyMethods.eachObject(ois, closure);
17118 }
17119
17120 @Deprecated
17121 public static <T> T withObjectInputStream(File file, Closure<T> closure) throws IOException {
17122 return ResourceGroovyMethods.withObjectInputStream(file, closure);
17123 }
17124
17125 @Deprecated
17126 public static <T> T withObjectInputStream(File file, ClassLoader classLoader, Closure<T> closure) throws IOException {
17127 return ResourceGroovyMethods.withObjectInputStream(file, classLoader, closure);
17128 }
17129
17130 @Deprecated
17131 public static <T> T withObjectInputStream(InputStream inputStream, Closure<T> closure) throws IOException {
17132 return IOGroovyMethods.withObjectInputStream(inputStream, closure);
17133 }
17134
17135 @Deprecated
17136 public static <T> T withObjectInputStream(InputStream inputStream, ClassLoader classLoader, Closure<T> closure) throws IOException {
17137 return IOGroovyMethods.withObjectInputStream(inputStream, classLoader, closure);
17138 }
17139
17140 @Deprecated
17141 public static <T> T eachLine(File self, Closure<T> closure) throws IOException {
17142 return ResourceGroovyMethods.eachLine(self, closure);
17143 }
17144
17145 @Deprecated
17146 public static <T> T eachLine(File self, String charset, Closure<T> closure) throws IOException {
17147 return ResourceGroovyMethods.eachLine(self, charset, closure);
17148 }
17149
17150 @Deprecated
17151 public static <T> T eachLine(File self, int firstLine, Closure<T> closure) throws IOException {
17152 return ResourceGroovyMethods.eachLine(self, firstLine, closure);
17153 }
17154
17155 @Deprecated
17156 public static <T> T eachLine(File self, String charset, int firstLine, Closure<T> closure) throws IOException {
17157 return ResourceGroovyMethods.eachLine(self, charset, firstLine, closure);
17158 }
17159
17160 @Deprecated
17161 public static <T> T eachLine(InputStream stream, String charset, Closure<T> closure) throws IOException {
17162 return IOGroovyMethods.eachLine(stream, charset, closure);
17163 }
17164
17165 @Deprecated
17166 public static <T> T eachLine(InputStream stream, String charset, int firstLine, Closure<T> closure) throws IOException {
17167 return IOGroovyMethods.eachLine(stream, charset, firstLine, closure);
17168 }
17169
17170 @Deprecated
17171 public static <T> T eachLine(InputStream stream, Closure<T> closure) throws IOException {
17172 return IOGroovyMethods.eachLine(stream, closure);
17173 }
17174
17175 @Deprecated
17176 public static <T> T eachLine(InputStream stream, int firstLine, Closure<T> closure) throws IOException {
17177 return IOGroovyMethods.eachLine(stream, firstLine, closure);
17178 }
17179
17180 @Deprecated
17181 public static <T> T eachLine(URL url, Closure<T> closure) throws IOException {
17182 return ResourceGroovyMethods.eachLine(url, closure);
17183 }
17184
17185 @Deprecated
17186 public static <T> T eachLine(URL url, int firstLine, Closure<T> closure) throws IOException {
17187 return ResourceGroovyMethods.eachLine(url, firstLine, closure);
17188 }
17189
17190 @Deprecated
17191 public static <T> T eachLine(URL url, String charset, Closure<T> closure) throws IOException {
17192 return ResourceGroovyMethods.eachLine(url, charset, closure);
17193 }
17194
17195 @Deprecated
17196 public static <T> T eachLine(URL url, String charset, int firstLine, Closure<T> closure) throws IOException {
17197 return ResourceGroovyMethods.eachLine(url, charset, firstLine, closure);
17198 }
17199
17200 @Deprecated
17201 public static <T> T eachLine(Reader self, Closure<T> closure) throws IOException {
17202 return IOGroovyMethods.eachLine(self, closure);
17203 }
17204
17205 @Deprecated
17206 public static <T> T eachLine(Reader self, int firstLine, Closure<T> closure) throws IOException {
17207 return IOGroovyMethods.eachLine(self, firstLine, closure);
17208 }
17209
17210 @Deprecated
17211 public static <T> T splitEachLine(File self, String regex, Closure<T> closure) throws IOException {
17212 return ResourceGroovyMethods.splitEachLine(self, regex, closure);
17213 }
17214
17215 @Deprecated
17216 public static <T> T splitEachLine(File self, Pattern pattern, Closure<T> closure) throws IOException {
17217 return ResourceGroovyMethods.splitEachLine(self, pattern, closure);
17218 }
17219
17220 @Deprecated
17221 public static <T> T splitEachLine(File self, String regex, String charset, Closure<T> closure) throws IOException {
17222 return ResourceGroovyMethods.splitEachLine(self, regex, charset, closure);
17223 }
17224
17225 @Deprecated
17226 public static <T> T splitEachLine(File self, Pattern pattern, String charset, Closure<T> closure) throws IOException {
17227 return ResourceGroovyMethods.splitEachLine(self, pattern, charset, closure);
17228 }
17229
17230 @Deprecated
17231 public static <T> T splitEachLine(URL self, String regex, Closure<T> closure) throws IOException {
17232 return ResourceGroovyMethods.splitEachLine(self, regex, closure);
17233 }
17234
17235 @Deprecated
17236 public static <T> T splitEachLine(URL self, Pattern pattern, Closure<T> closure) throws IOException {
17237 return ResourceGroovyMethods.splitEachLine(self, pattern, closure);
17238 }
17239
17240 @Deprecated
17241 public static <T> T splitEachLine(URL self, String regex, String charset, Closure<T> closure) throws IOException {
17242 return ResourceGroovyMethods.splitEachLine(self, regex, charset, closure);
17243 }
17244
17245 @Deprecated
17246 public static <T> T splitEachLine(URL self, Pattern pattern, String charset, Closure<T> closure) throws IOException {
17247 return ResourceGroovyMethods.splitEachLine(self, pattern, charset, closure);
17248 }
17249
17250 @Deprecated
17251 public static <T> T splitEachLine(Reader self, String regex, Closure<T> closure) throws IOException {
17252 return IOGroovyMethods.splitEachLine(self, regex, closure);
17253 }
17254
17255 @Deprecated
17256 public static <T> T splitEachLine(Reader self, Pattern pattern, Closure<T> closure) throws IOException {
17257 return IOGroovyMethods.splitEachLine(self, pattern, closure);
17258 }
17259
17260 @Deprecated
17261 public static <T> T splitEachLine(InputStream stream, String regex, String charset, Closure<T> closure) throws IOException {
17262 return IOGroovyMethods.splitEachLine(stream, charset, regex, closure);
17263 }
17264
17265 @Deprecated
17266 public static <T> T splitEachLine(InputStream stream, Pattern pattern, String charset, Closure<T> closure) throws IOException {
17267 return IOGroovyMethods.splitEachLine(stream, pattern, charset, closure);
17268 }
17269
17270 @Deprecated
17271 public static <T> T splitEachLine(InputStream stream, String regex, Closure<T> closure) throws IOException {
17272 return IOGroovyMethods.splitEachLine(stream, regex, closure);
17273 }
17274
17275 @Deprecated
17276 public static <T> T splitEachLine(InputStream stream, Pattern pattern, Closure<T> closure) throws IOException {
17277 return IOGroovyMethods.splitEachLine(stream, pattern, closure);
17278 }
17279
17280 @Deprecated
17281 public static String readLine(Reader self) throws IOException {
17282 return IOGroovyMethods.readLine(self);
17283 }
17284
17285 @Deprecated
17286 public static List<String> readLines(File file) throws IOException {
17287 return ResourceGroovyMethods.readLines(file);
17288 }
17289
17290 @Deprecated
17291 public static List<String> readLines(File file, String charset) throws IOException {
17292 return ResourceGroovyMethods.readLines(file, charset);
17293 }
17294
17295 @Deprecated
17296 public static List<String> readLines(InputStream stream) throws IOException {
17297 return IOGroovyMethods.readLines(stream);
17298 }
17299
17300 @Deprecated
17301 public static List<String> readLines(InputStream stream, String charset) throws IOException {
17302 return IOGroovyMethods.readLines(stream, charset);
17303 }
17304
17305 @Deprecated
17306 public static List<String> readLines(URL self) throws IOException {
17307 return ResourceGroovyMethods.readLines(self);
17308 }
17309
17310 @Deprecated
17311 public static List<String> readLines(URL self, String charset) throws IOException {
17312 return ResourceGroovyMethods.readLines(self, charset);
17313 }
17314
17315 @Deprecated
17316 public static List<String> readLines(Reader reader) throws IOException {
17317 return IOGroovyMethods.readLines(reader);
17318 }
17319
17320 @Deprecated
17321 public static String getText(File file, String charset) throws IOException {
17322 return ResourceGroovyMethods.getText(file, charset);
17323 }
17324
17325 @Deprecated
17326 public static String getText(File file) throws IOException {
17327 return ResourceGroovyMethods.getText(file);
17328 }
17329
17330 @Deprecated
17331 public static String getText(URL url) throws IOException {
17332 return ResourceGroovyMethods.getText(url);
17333 }
17334
17335 @Deprecated
17336 public static String getText(URL url, Map parameters) throws IOException {
17337 return ResourceGroovyMethods.getText(url, parameters);
17338 }
17339
17340 @Deprecated
17341 public static String getText(URL url, String charset) throws IOException {
17342 return ResourceGroovyMethods.getText(url, charset);
17343 }
17344
17345 @Deprecated
17346 public static String getText(URL url, Map parameters, String charset) throws IOException {
17347 return ResourceGroovyMethods.getText(url, parameters, charset);
17348 }
17349
17350 @Deprecated
17351 public static String getText(InputStream is) throws IOException {
17352 return IOGroovyMethods.getText(is);
17353 }
17354
17355 @Deprecated
17356 public static String getText(InputStream is, String charset) throws IOException {
17357 return IOGroovyMethods.getText(is, charset);
17358 }
17359
17360 @Deprecated
17361 public static String getText(Reader reader) throws IOException {
17362 return IOGroovyMethods.getText(reader);
17363 }
17364
17365 @Deprecated
17366 public static String getText(BufferedReader reader) throws IOException {
17367 return IOGroovyMethods.getText(reader);
17368 }
17369
17370 @Deprecated
17371 public static byte[] getBytes(File file) throws IOException {
17372 return ResourceGroovyMethods.getBytes(file);
17373 }
17374
17375 @Deprecated
17376 public static byte[] getBytes(URL url) throws IOException {
17377 return ResourceGroovyMethods.getBytes(url);
17378 }
17379
17380 @Deprecated
17381 public static byte[] getBytes(InputStream is) throws IOException {
17382 return IOGroovyMethods.getBytes(is);
17383 }
17384
17385 @Deprecated
17386 public static void setBytes(File file, byte[] bytes) throws IOException {
17387 ResourceGroovyMethods.setBytes(file, bytes);
17388 }
17389
17390 @Deprecated
17391 public static void setBytes(OutputStream os, byte[] bytes) throws IOException {
17392 IOGroovyMethods.setBytes(os, bytes);
17393 }
17394
17395 @Deprecated
17396 public static void writeLine(BufferedWriter writer, String line) throws IOException {
17397 IOGroovyMethods.writeLine(writer, line);
17398 }
17399
17400 @Deprecated
17401 public static void write(File file, String text) throws IOException {
17402 ResourceGroovyMethods.write(file, text);
17403 }
17404
17405 @Deprecated
17406 public static void setText(File file, String text) throws IOException {
17407 ResourceGroovyMethods.setText(file, text);
17408 }
17409
17410 @Deprecated
17411 public static void setText(File file, String text, String charset) throws IOException {
17412 ResourceGroovyMethods.setText(file, text, charset);
17413 }
17414
17415 @Deprecated
17416 public static File leftShift(File file, Object text) throws IOException {
17417 return ResourceGroovyMethods.leftShift(file, text);
17418 }
17419
17420 @Deprecated
17421 public static File leftShift(File file, byte[] bytes) throws IOException {
17422 return ResourceGroovyMethods.leftShift(file, bytes);
17423 }
17424
17425 @Deprecated
17426 public static File leftShift(File file, InputStream data) throws IOException {
17427 return ResourceGroovyMethods.leftShift(file, data);
17428 }
17429
17430 @Deprecated
17431 public static void write(File file, String text, String charset) throws IOException {
17432 ResourceGroovyMethods.write(file, text, charset);
17433 }
17434
17435 @Deprecated
17436 public static void append(File file, Object text) throws IOException {
17437 ResourceGroovyMethods.append(file, text);
17438 }
17439
17440 @Deprecated
17441 public static void append(File file, byte[] bytes) throws IOException {
17442 ResourceGroovyMethods.append(file, bytes);
17443 }
17444
17445 @Deprecated
17446 public static void append(File self, InputStream stream ) throws IOException {
17447 ResourceGroovyMethods.append(self, stream);
17448 }
17449
17450 @Deprecated
17451 public static void append(File file, Object text, String charset) throws IOException {
17452 ResourceGroovyMethods.append(file, text, charset);
17453 }
17454
17455 @Deprecated
17456 public static void eachFile(final File self, final FileType fileType, final Closure closure)
17457 throws FileNotFoundException, IllegalArgumentException {
17458 ResourceGroovyMethods.eachFile(self, fileType, closure);
17459 }
17460
17461 @Deprecated
17462 public static void eachFile(final File self, final Closure closure) throws FileNotFoundException, IllegalArgumentException {
17463 ResourceGroovyMethods.eachFile(self, closure);
17464 }
17465
17466 @Deprecated
17467 public static void eachDir(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException {
17468 ResourceGroovyMethods.eachDir(self, closure);
17469 }
17470
17471 @Deprecated
17472 public static void eachFileRecurse(final File self, final FileType fileType, final Closure closure)
17473 throws FileNotFoundException, IllegalArgumentException {
17474 ResourceGroovyMethods.eachFileRecurse(self, fileType, closure);
17475 }
17476
17477 @Deprecated
17478 public static void traverse(final File self, final Map<String, Object> options, final Closure closure)
17479 throws FileNotFoundException, IllegalArgumentException {
17480 ResourceGroovyMethods.traverse(self, options, closure);
17481 }
17482
17483 @Deprecated
17484 public static void traverse(final File self, final Closure closure) throws FileNotFoundException, IllegalArgumentException {
17485 ResourceGroovyMethods.traverse(self, closure);
17486 }
17487
17488 @Deprecated
17489 public static void traverse(final File self, final Map<String, Object> options)
17490 throws FileNotFoundException, IllegalArgumentException {
17491 ResourceGroovyMethods.traverse(self, options);
17492 }
17493
17494 @Deprecated
17495 public static void eachFileRecurse(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException {
17496 ResourceGroovyMethods.eachFileRecurse(self, closure);
17497 }
17498
17499 @Deprecated
17500 public static void eachDirRecurse(final File self, final Closure closure) throws FileNotFoundException, IllegalArgumentException {
17501 ResourceGroovyMethods.eachDirRecurse(self, closure);
17502 }
17503
17504 @Deprecated
17505 public static void eachFileMatch(final File self, final FileType fileType, final Object nameFilter, final Closure closure)
17506 throws FileNotFoundException, IllegalArgumentException {
17507 ResourceGroovyMethods.eachFileMatch(self, fileType, nameFilter, closure);
17508 }
17509
17510 @Deprecated
17511 public static void eachFileMatch(final File self, final Object nameFilter, final Closure closure)
17512 throws FileNotFoundException, IllegalArgumentException {
17513 ResourceGroovyMethods.eachFileMatch(self, nameFilter, closure);
17514 }
17515
17516 @Deprecated
17517 public static void eachDirMatch(final File self, final Object nameFilter, final Closure closure) throws FileNotFoundException, IllegalArgumentException {
17518 ResourceGroovyMethods.eachDirMatch(self, nameFilter, closure);
17519 }
17520
17521 @Deprecated
17522 public static boolean deleteDir(final File self) {
17523 return ResourceGroovyMethods.deleteDir(self);
17524 }
17525
17526 @Deprecated
17527 public static boolean renameTo(final File self, String newPathName) {
17528 return ResourceGroovyMethods.renameTo(self, newPathName);
17529 }
17530
17531 @Deprecated
17532 public static Iterator<String> iterator(Reader self) {
17533 return IOGroovyMethods.iterator(self);
17534 }
17535
17536 @Deprecated
17537 public static Iterator<Byte> iterator(InputStream self) {
17538 return IOGroovyMethods.iterator(self);
17539 }
17540
17541 @Deprecated
17542 public static Iterator<Byte> iterator(final DataInputStream self) {
17543 return IOGroovyMethods.iterator(self);
17544 }
17545
17546 @Deprecated
17547 public static File asWritable(File file) {
17548 return ResourceGroovyMethods.asWritable(file);
17549 }
17550
17551 @Deprecated
17552 public static <T> T asType(File f, Class<T> c) {
17553 return ResourceGroovyMethods.asType(f, c);
17554 }
17555
17556 @Deprecated
17557 public static File asWritable(File file, String encoding) {
17558 return ResourceGroovyMethods.asWritable(file, encoding);
17559 }
17560
17561 @Deprecated
17562 public static BufferedReader newReader(File file) throws IOException {
17563 return ResourceGroovyMethods.newReader(file);
17564 }
17565
17566 @Deprecated
17567 public static BufferedReader newReader(File file, String charset)
17568 throws FileNotFoundException, UnsupportedEncodingException {
17569 return ResourceGroovyMethods.newReader(file, charset);
17570 }
17571
17572 @Deprecated
17573 public static BufferedReader newReader(InputStream self) {
17574 return IOGroovyMethods.newReader(self);
17575 }
17576
17577 @Deprecated
17578 public static BufferedReader newReader(InputStream self, String charset) throws UnsupportedEncodingException {
17579 return IOGroovyMethods.newReader(self, charset);
17580 }
17581
17582 @Deprecated
17583 public static <T> T withReader(File file, Closure<T> closure) throws IOException {
17584 return ResourceGroovyMethods.withReader(file, closure);
17585 }
17586
17587 @Deprecated
17588 public static <T> T withReader(File file, String charset, Closure<T> closure) throws IOException {
17589 return ResourceGroovyMethods.withReader(file, charset, closure);
17590 }
17591
17592 @Deprecated
17593 public static BufferedOutputStream newOutputStream(File file) throws IOException {
17594 return ResourceGroovyMethods.newOutputStream(file);
17595 }
17596
17597 @Deprecated
17598 public static DataOutputStream newDataOutputStream(File file) throws IOException {
17599 return ResourceGroovyMethods.newDataOutputStream(file);
17600 }
17601
17602 @Deprecated
17603 public static Object withOutputStream(File file, Closure closure) throws IOException {
17604 return ResourceGroovyMethods.withOutputStream(file, closure);
17605 }
17606
17607 @Deprecated
17608 public static Object withInputStream(File file, Closure closure) throws IOException {
17609 return ResourceGroovyMethods.withInputStream(file, closure);
17610 }
17611
17612 @Deprecated
17613 public static <T> T withInputStream(URL url, Closure<T> closure) throws IOException {
17614 return ResourceGroovyMethods.withInputStream(url, closure);
17615 }
17616
17617 @Deprecated
17618 public static <T> T withDataOutputStream(File file, Closure<T> closure) throws IOException {
17619 return ResourceGroovyMethods.withDataOutputStream(file, closure);
17620 }
17621
17622 @Deprecated
17623 public static <T> T withDataInputStream(File file, Closure<T> closure) throws IOException {
17624 return ResourceGroovyMethods.withDataInputStream(file, closure);
17625 }
17626
17627 @Deprecated
17628 public static BufferedWriter newWriter(File file) throws IOException {
17629 return ResourceGroovyMethods.newWriter(file);
17630 }
17631
17632 @Deprecated
17633 public static BufferedWriter newWriter(File file, boolean append) throws IOException {
17634 return ResourceGroovyMethods.newWriter(file, append);
17635 }
17636
17637 @Deprecated
17638 public static BufferedWriter newWriter(File file, String charset, boolean append) throws IOException {
17639 return ResourceGroovyMethods.newWriter(file, charset, append);
17640 }
17641
17642 @Deprecated
17643 public static BufferedWriter newWriter(File file, String charset) throws IOException {
17644 return ResourceGroovyMethods.newWriter(file, charset);
17645 }
17646
17647 @Deprecated
17648 public static <T> T withWriter(File file, Closure<T> closure) throws IOException {
17649 return ResourceGroovyMethods.withWriter(file, closure);
17650 }
17651
17652 @Deprecated
17653 public static <T> T withWriter(File file, String charset, Closure<T> closure) throws IOException {
17654 return ResourceGroovyMethods.withWriter(file, charset, closure);
17655 }
17656
17657 @Deprecated
17658 public static <T> T withWriterAppend(File file, String charset, Closure<T> closure) throws IOException {
17659 return ResourceGroovyMethods.withWriterAppend(file, charset, closure);
17660 }
17661
17662 @Deprecated
17663 public static <T> T withWriterAppend(File file, Closure<T> closure) throws IOException {
17664 return ResourceGroovyMethods.withWriterAppend(file, closure);
17665 }
17666
17667 @Deprecated
17668 public static PrintWriter newPrintWriter(File file) throws IOException {
17669 return ResourceGroovyMethods.newPrintWriter(file);
17670 }
17671
17672 @Deprecated
17673 public static PrintWriter newPrintWriter(File file, String charset) throws IOException {
17674 return ResourceGroovyMethods.newPrintWriter(file, charset);
17675 }
17676
17677 @Deprecated
17678 public static PrintWriter newPrintWriter(Writer writer) {
17679 return IOGroovyMethods.newPrintWriter(writer);
17680 }
17681
17682 @Deprecated
17683 public static <T> T withPrintWriter(File file, Closure<T> closure) throws IOException {
17684 return ResourceGroovyMethods.withPrintWriter(file, closure);
17685 }
17686
17687 @Deprecated
17688 public static <T> T withPrintWriter(File file, String charset, Closure<T> closure) throws IOException {
17689 return ResourceGroovyMethods.withPrintWriter(file, charset, closure);
17690 }
17691
17692 @Deprecated
17693 public static <T> T withPrintWriter(Writer writer, Closure<T> closure) throws IOException {
17694 return IOGroovyMethods.withPrintWriter(writer, closure);
17695 }
17696
17697 @Deprecated
17698 public static <T> T withWriter(Writer writer, Closure<T> closure) throws IOException {
17699 return IOGroovyMethods.withWriter(writer, closure);
17700 }
17701
17702 @Deprecated
17703 public static <T> T withReader(Reader reader, Closure<T> closure) throws IOException {
17704 return IOGroovyMethods.withReader(reader, closure);
17705 }
17706
17707 @Deprecated
17708 public static <T> T withStream(InputStream stream, Closure<T> closure) throws IOException {
17709 return IOGroovyMethods.withStream(stream, closure);
17710 }
17711
17712 @Deprecated
17713 public static <T> T withReader(URL url, Closure<T> closure) throws IOException {
17714 return ResourceGroovyMethods.withReader(url, closure);
17715 }
17716
17717 @Deprecated
17718 public static <T> T withReader(URL url, String charset, Closure<T> closure) throws IOException {
17719 return ResourceGroovyMethods.withReader(url, charset, closure);
17720 }
17721
17722 @Deprecated
17723 public static <T> T withReader(InputStream in, Closure<T> closure) throws IOException {
17724 return IOGroovyMethods.withReader(in, closure);
17725 }
17726
17727 @Deprecated
17728 public static <T> T withReader(InputStream in, String charset, Closure<T> closure) throws IOException {
17729 return IOGroovyMethods.withReader(in, charset, closure);
17730 }
17731
17732 @Deprecated
17733 public static <T> T withWriter(OutputStream stream, Closure<T> closure) throws IOException {
17734 return IOGroovyMethods.withWriter(stream, closure);
17735 }
17736
17737 @Deprecated
17738 public static <T> T withWriter(OutputStream stream, String charset, Closure<T> closure) throws IOException {
17739 return IOGroovyMethods.withWriter(stream, charset, closure);
17740 }
17741
17742 @Deprecated
17743 public static <T> T withStream(OutputStream os, Closure<T> closure) throws IOException {
17744 return IOGroovyMethods.withStream(os, closure);
17745 }
17746
17747 @Deprecated
17748 public static BufferedInputStream newInputStream(File file) throws FileNotFoundException {
17749 return ResourceGroovyMethods.newInputStream(file);
17750 }
17751
17752 @Deprecated
17753 public static BufferedInputStream newInputStream(URL url) throws MalformedURLException, IOException {
17754 return ResourceGroovyMethods.newInputStream(url);
17755 }
17756
17757 @Deprecated
17758 public static BufferedInputStream newInputStream(URL url, Map parameters) throws MalformedURLException, IOException {
17759 return ResourceGroovyMethods.newInputStream(url, parameters);
17760 }
17761
17762 @Deprecated
17763 public static BufferedReader newReader(URL url) throws MalformedURLException, IOException {
17764 return ResourceGroovyMethods.newReader(url);
17765 }
17766
17767 @Deprecated
17768 public static BufferedReader newReader(URL url, Map parameters) throws MalformedURLException, IOException {
17769 return ResourceGroovyMethods.newReader(url, parameters);
17770 }
17771
17772 @Deprecated
17773 public static BufferedReader newReader(URL url, String charset) throws MalformedURLException, IOException {
17774 return ResourceGroovyMethods.newReader(url, charset);
17775 }
17776
17777 @Deprecated
17778 public static BufferedReader newReader(URL url, Map parameters, String charset) throws MalformedURLException, IOException {
17779 return ResourceGroovyMethods.newReader(url, parameters, charset);
17780 }
17781
17782 @Deprecated
17783 public static DataInputStream newDataInputStream(File file) throws FileNotFoundException {
17784 return ResourceGroovyMethods.newDataInputStream(file);
17785 }
17786
17787 @Deprecated
17788 public static void eachByte(File self, Closure closure) throws IOException {
17789 ResourceGroovyMethods.eachByte(self, closure);
17790 }
17791
17792 @Deprecated
17793 public static void eachByte(File self, int bufferLen, Closure closure) throws IOException {
17794 ResourceGroovyMethods.eachByte(self, bufferLen, closure);
17795 }
17796
17797 @Deprecated
17798 public static void eachByte(InputStream is, Closure closure) throws IOException {
17799 IOGroovyMethods.eachByte(is, closure);
17800 }
17801
17802 @Deprecated
17803 public static void eachByte(InputStream is, int bufferLen, Closure closure) throws IOException {
17804 IOGroovyMethods.eachByte(is, bufferLen, closure);
17805 }
17806
17807 @Deprecated
17808 public static void eachByte(URL url, Closure closure) throws IOException {
17809 ResourceGroovyMethods.eachByte(url, closure);
17810 }
17811
17812 @Deprecated
17813 public static void eachByte(URL url, int bufferLen, Closure closure) throws IOException {
17814 ResourceGroovyMethods.eachByte(url, bufferLen, closure);
17815 }
17816
17817 @Deprecated
17818 public static void transformChar(Reader self, Writer writer, Closure closure) throws IOException {
17819 IOGroovyMethods.transformChar(self, writer, closure);
17820 }
17821
17822 @Deprecated
17823 public static void transformLine(Reader reader, Writer writer, Closure closure) throws IOException {
17824 IOGroovyMethods.transformLine(reader, writer, closure);
17825 }
17826
17827 @Deprecated
17828 public static void filterLine(Reader reader, Writer writer, Closure closure) throws IOException {
17829 IOGroovyMethods.filterLine(reader, writer, closure);
17830 }
17831
17832 @Deprecated
17833 public static Writable filterLine(File self, Closure closure) throws IOException {
17834 return ResourceGroovyMethods.filterLine(self, closure);
17835 }
17836
17837 @Deprecated
17838 public static Writable filterLine(File self, String charset, Closure closure) throws IOException {
17839 return ResourceGroovyMethods.filterLine(self, closure);
17840 }
17841
17842 @Deprecated
17843 public static void filterLine(File self, Writer writer, Closure closure) throws IOException {
17844 ResourceGroovyMethods.filterLine(self, writer, closure);
17845 }
17846
17847 @Deprecated
17848 public static void filterLine(File self, Writer writer, String charset, Closure closure) throws IOException {
17849 ResourceGroovyMethods.filterLine(self, writer, charset, closure);
17850 }
17851
17852 @Deprecated
17853 public static Writable filterLine(Reader reader, final Closure closure) {
17854 return IOGroovyMethods.filterLine(reader, closure);
17855 }
17856
17857 @Deprecated
17858 public static Writable filterLine(InputStream self, Closure predicate) {
17859 return IOGroovyMethods.filterLine(self, predicate);
17860 }
17861
17862 @Deprecated
17863 public static Writable filterLine(InputStream self, String charset, Closure predicate) throws UnsupportedEncodingException {
17864 return IOGroovyMethods.filterLine(self, charset, predicate);
17865 }
17866
17867 @Deprecated
17868 public static void filterLine(InputStream self, Writer writer, Closure predicate) throws IOException {
17869 IOGroovyMethods.filterLine(self, writer, predicate);
17870 }
17871
17872 @Deprecated
17873 public static void filterLine(InputStream self, Writer writer, String charset, Closure predicate) throws IOException {
17874 IOGroovyMethods.filterLine(self, writer, charset, predicate);
17875 }
17876
17877 @Deprecated
17878 public static Writable filterLine(URL self, Closure predicate) throws IOException {
17879 return ResourceGroovyMethods.filterLine(self, predicate);
17880 }
17881
17882 @Deprecated
17883 public static Writable filterLine(URL self, String charset, Closure predicate) throws IOException {
17884 return ResourceGroovyMethods.filterLine(self, charset, predicate);
17885 }
17886
17887 @Deprecated
17888 public static void filterLine(URL self, Writer writer, Closure predicate) throws IOException {
17889 ResourceGroovyMethods.filterLine(self, writer, predicate);
17890 }
17891
17892 @Deprecated
17893 public static void filterLine(URL self, Writer writer, String charset, Closure predicate) throws IOException {
17894 ResourceGroovyMethods.filterLine(self, writer, charset, predicate);
17895 }
17896
17897 @Deprecated
17898 public static byte[] readBytes(File file) throws IOException {
17899 return ResourceGroovyMethods.readBytes(file);
17900 }
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911 public static Object withTraits(Object self, Class<?>... traits) {
17912 List<Class> interfaces = new ArrayList<Class>();
17913 Collections.addAll(interfaces, traits);
17914 return ProxyGenerator.INSTANCE.instantiateDelegate(interfaces, self);
17915 }
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932 public static <T> List<T> swap(List<T> self, int i, int j) {
17933 Collections.swap(self, i, j);
17934 return self;
17935 }
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951 public static <T> T[] swap(T[] self, int i, int j) {
17952 T tmp = self[i];
17953 self[i] = self[j];
17954 self[j] = tmp;
17955 return self;
17956 }
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972 public static boolean[] swap(boolean[] self, int i, int j) {
17973 boolean tmp = self[i];
17974 self[i] = self[j];
17975 self[j] = tmp;
17976 return self;
17977 }
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993 public static byte[] swap(byte[] self, int i, int j) {
17994 byte tmp = self[i];
17995 self[i] = self[j];
17996 self[j] = tmp;
17997 return self;
17998 }
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014 public static char[] swap(char[] self, int i, int j) {
18015 char tmp = self[i];
18016 self[i] = self[j];
18017 self[j] = tmp;
18018 return self;
18019 }
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035 public static double[] swap(double[] self, int i, int j) {
18036 double tmp = self[i];
18037 self[i] = self[j];
18038 self[j] = tmp;
18039 return self;
18040 }
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056 public static float[] swap(float[] self, int i, int j) {
18057 float tmp = self[i];
18058 self[i] = self[j];
18059 self[j] = tmp;
18060 return self;
18061 }
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077 public static int[] swap(int[] self, int i, int j) {
18078 int tmp = self[i];
18079 self[i] = self[j];
18080 self[j] = tmp;
18081 return self;
18082 }
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098 public static long[] swap(long[] self, int i, int j) {
18099 long tmp = self[i];
18100 self[i] = self[j];
18101 self[j] = tmp;
18102 return self;
18103 }
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119 public static short[] swap(short[] self, int i, int j) {
18120 short tmp = self[i];
18121 self[i] = self[j];
18122 self[j] = tmp;
18123 return self;
18124 }
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143 public static <E> E removeAt(List<E> self, int index) {
18144 return self.remove(index);
18145 }
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164 public static <E> boolean removeElement(Collection<E> self, Object o) {
18165 return self.remove(o);
18166 }
18167 }